📜 ⬆️ ⬇️

Distributing freebies: non-braking threads in Java. Project loom

Do you want threads in the javka that do not eat the memory but do not slow down? Good laudable desire, and this issue answers this question.


We explain the work of Project Loom on pizza boxes! Fly!


Delivery scope:



All this is removed and written specifically for Habr .





We live in a cruel new world where likes are worth more than money. A blogger can do almost anything for likes. The final stage of international capitalism and technological promiscuity.


I know that your laychik gave you hard work. It is not known what abominations you generally do to make money. Perhaps you are responding to comments that you don’t really like, to people you hate, and so earn yourself the coveted plus signs. I do not want to know about it at all. Huskies do not smell. Just give me a little bit of likes, and I pretend I don’t know where they are from. That's all that matters to a sales blogger like me.


All little kids will go to create new content. Thank.


Call Signs


Hi, javana, Oleg is in touch.


Do you often see such a picture on your web service: at first everything was fine, then a million Chinese came to you, the service did a million threads and choked to hell with the dogs?





Do you want just such a nice picture?





In other words, do you want threads in javka that do not guzzle memory but do not slow down? Good laudable desire, and this issue answers this question.


We will deal, in fact, with the unpacking of the new framework . Remember how Wylsacom unpacked iPhones? Some people no longer remember old reviewers, but why? Because Habr is a mainstream stronghold, and the paid vidos are, excuse me, the rays of diarrhea . In this post we will deal exclusively with technical hardcore.


First two minutes for a string, a disclaimer and other stuff that needs to be said. You can skip it if you are too lazy.


First of all, everything said in the video is my personal thoughts, and they have nothing to do with the employer or the glorious Oracle Corporation, the world government of lizards and the line in a mortar. I even write it at three o'clock in the morning, so that it was clearly clear that this is my personal initiative, my personal garbage. All matches are purely random.


But there is another bonus goal. We constantly talk about korutinakh in Kotlin. Recently there were interviews with Roma Elizarov , the god Korutin, and with Pasha Finkelstein , who is going to write backends for them. Soon there will be an interview with Andrei Breslav - who is the father of Kotlin. And everywhere the project Loom is somehow mentioned, because it is an analogue of corutin. And if you don’t know what a Loom is, you may become dumb when reading these interviews. There are some cool dudes, they discuss cool things. And you are, and you are not with them, you are a schmuck. This is very stupid.


Do not do so, read what Loom is, in this article, or continue to watch this video, I'll explain everything.


So, what's the plot. There is such a dude, Ron Pressler.


')




Last year, he went to the newsletter , stated that the threads in java sucked, and suggested that I run the runtime and fix it. And everyone would laugh at him and throw stones at him, by shit, if not for the fact that he had previously written Quasar , and this is actually very cool. You can swear at Quasar for a long time, but it seems to be there and it works, and in the big picture of all this is rather an achievement.


There are a damn number of govnoderov who do nothing, just say. Well, understand correctly, I'm the same. Or there are people who seem to be cool engineers, but in general, unconsciously, they say: “In Java it is necessary to improve the threads.” What to improve? What are threads?


People in general are too lazy to think.


How is the joke:
Flying in the plane Petka and Vasily Ivanovich.
Vasily Ivanovich asks: - Petka, devices?
Petka answers: - 200!
Vasily Ivanovich: - And what about 200?
Petka: - And what about the devices?


I'll tell the story. I was in Ukraine this spring, we flew from Belarus (you understand why it is impossible directly from St. Petersburg). And at the customs, we sat for about two hours, no less. The customs officers are very nice, seriously asking if Java is an outdated technology. Nearby were people who fly on the same konfu. And I'm the same type of speaker, I have to pantanutsya, I stand and, as expected, shamelessly talk about things that I do not use at all. Along the way, I talked about the JDK distribution called Liberica, which is such a JDK for the Raspberry Pi.


And what do you think. Not half a year goes by, as people are knocking on my cart and saying that, look, we wrote a solution in Prod on Liberik, and I already have a report about this on the Belarusian jfuture.by. This is the approach. This is not some lousy evangelist, but man, man, man, man, man.


By the way, we will soon have the Joker 2018 conference , which will include Andrei Breslav (obviously, fumbling in the korutinakh), and Pasha Finkelstein , and you can ask Josh Long about supporting Loom in Spring. Well and still a lot of cool prominent experts, fly!

And now, going back to threads. People are trying to hold a thought through their degraded two neurons, they wind snot on their fists, and mumble: "The threads are not right in Java, the threads are not right in Java." Devices! What appliances? This is generally hell.


And here comes Presler, a normal dude, not degraded, and first makes a sane description. A year later, sawing a working demo. All this I said, so that you understand that a normal description of problems, normal documentation is such a special kind of heroism. A demo - it's all space. This is the first person who ever did something in this direction. He needs the most.


Together with the demo, Pressler spoke at the conference and released the following video:



In fact, this entire article is a review of what was said there. I do not pretend to the uniqueness of this material, everything that is in this article came up with Ron.


The discussion is about three painful topics:



Probably, he was so zadolbalo sawed Quasar and fight with his glitches, that now there is no strength - you have to push it into runtime.


It was a year ago, and since then they sawed a prototype. Some have already lost hope that we will see the demo sometime, but a month ago they gave birth to her and showed what is visible on this tweet .






All three painful topics in this demo are either directly in the code, or at least are present morally. Well, yes, they haven’t mastered tail calls yet, but they want to.


Problematics


Unhappy users, application developers, when they make an API, are forced to choose between two chairs. Peaks are embedded in one chair, and flowers grow on the other. And neither one nor the other does not suit us.






For example, if you write a service that works synchronously, it works fine with legacy code, it is easy to debug and monitor the performance. Problems will arise with bandwidth and scalability. Just because the number of threads that can now be run on a simple piece of hardware, on the commodity hardware - well, let's say, two thousand. This is much less than the number of connections that can be opened to this server. Which in terms of netcode can be almost infinite.


(Well, yes, this is somehow related to the fact that the sockets in Java are designed as a retard, but this is a topic for another conversation)


Imagine you are writing some MMO.






For example, during the Northern War in EVE Online, two thousand four hundred pilots gathered at one point in space, each of which - conditionally, if it were written in Java - there would be more than one thread. And the pilot, of course, is a complex business logic, and not the issuance of any HTML, which can be struck in the magnifying glass.


The response time in that battle was so long that the player had to wait a few minutes to wait for a shot. As far as I know, CCP specifically threw the enormous hardware resources of its cluster for that battle.


Although, I, probably, use EVE as an example in vain, because, as far as I understand, everything is written in Python, and in Python with multithreading it is still worse than ours - and it can be considered a bad concurrence of language features. But the example is clear and with pictures.


If you are interested in the subject of MMOs in general and the history of the "Northern War" in particular, recently a very good video on this subject appeared on the BULDJAT channel (whatever the name means), look from my time mark.



Back to the topic.


On the other hand, you can use any asynchronous framework. It scales. But we will immediately get to a very complex debugging, complex profiling of the performance, we will not be able to integrate it seamlessly with Legacy, we will have to rewrite a lot of things, wrap in nasty wrappers, and generally feel like we were just raped. Several times in a row. All day long, in fact, all the time while we are writing this, we will have to feel this way.


I asked an expert, a well-known academician Escobar, what he thinks about this:






What to do? So-called faybery are in a hurry to help.


In general, faibers are such light threads that also scoop address space (because there are no miracles, you understand). But unlike conventional threads, they are not using preemptive multitasking, but cooperative multitasking. More worth reading on Wikipedia .


Faybers can realize the advantages of both synchronous and asynchronous programming. As a result, hardware utilization is improved, and we use fewer servers in the cluster for the same task. Well, in the pocket for this we get lavender. Babosy. Lave Money. Well, you understand. For saved servers.


At the crossroads


The first thing I want to discuss. People do not understand the difference between continuations and faibers.
Now will be Cultural enlightenment!


Let's announce the fact: Continuation and Fiber are different things.


Continuations


Fiber is built on top of a mechanics called Continuations.


Continuations (more precisely, delimited continuations) is a kind of calculation, execution, a piece of a program that can fall asleep, then wake up and continue execution from the place where you fell asleep. Sometimes he can even be cloned or serialized, even while he is sleeping.


I will use the word “continuation”, not “continuation” (as it is written on Wikipedia), because we all communicate in Ruglish . Using normal Russian terminology, one can easily come to a situation when the difference between a Russian and an English term becomes too large and no one else understands the meaning of what has been said.


I will also sometimes use the word “crowding out” instead of the English version of “yield”. Just the word "yield" - it is some kind of really very gruesome. Therefore, there will be "crowding out."


So here. It is very important that there should be no concarrency within the continuum. It is in itself - the minimum primitive of this process.


Runnable, pause(). , . , , , , . . .


— . , .


java.base, . (src/java.base/share/classes/java/lang/Continuation.java). , , - .


public class Continuation implements Runnable {
    public Continuation(ContinuationScope scope, Runnable body);
    public final void run();
    public static void yield(ContinuationScope scope);
    public boolean isDone();

    protected void onPinned(Reason reason) {
        throw new IllegalStateException("Pinned: " + reason);
    }
}

, . , Runnable. .


. body — , , scope — , .


, run, - yield ( - , ). isDone, .


, ( , ), yield. , , . , , , . , , … , , - . .


:


Continuation cont = new Continuation(SCOPE, () -> {
        while (true) {
            System.out.println("before");
            Continuation.yield(SCOPE);
            System.out.println("after");
        }
    });

while (!cont.isDone()) {
    cont.run();
}

. , «» , - .


, , — .


, , API. . Spring Framework , . . . , , . . , . , - . — Spring, , .


.


Fibers


, .

, :



. , Kotlin , . . — .


, JVM , , — . API, «»: , Reactor, Spring Project Reactor, -- , .


, .


. :



:







, . , .



-.







java.util.concurrent.Executor, — ForkJoinPool. . - , .


:



:



.


:




mount();
try {
    cont.run();
} finally () {
    unmount();
}


ForkJoinPool - ( ).



Fiber f = Fiber.execute( () -> {
    System.out.println("Good Morning!");
    readLock.lock();
    try {
        System.out.println("Good Afternoon");
    } finally {
        readLock.unlock();
    }
    System.out.println("Good Night");
});

, , :



.


. Project Loom , readLock.lock(); . , .


, !


, .


- , .





, .


,





, , , .





, .


, .


-, - , .





, , . .





, , , Continuation.run. — .


, - .





, - , .


, , , .
, , , . .



, .





- . !


.





!!! , Continuation.yield.





:





, continuation.run





, .





.






, ? , ?


- , . . , , .


, , .



- ? , ! — .




. , , , . -, OpenJDK — , .


? — 2 .


—


PrivilegedAction<Void> pa = () -> {
    readLock.lock(); // may park/yield
    try {
        //
    } finally {
        readLock.unlock();
    }
    return null;
}
AccessController.doPrivileged(pa); //native method

doPrivileged .


doPrivileged, VM, , readLock.lock(). - , . . -, , .


, .


— synchronized-



synchronized (object) { //may park
    object.wait(); //may park
}

synchronized (object) { //may park
    socket.getInputStream().read(); //may park
}

, - .


, , wait+notify condition objects, ? .


Thread API? Thread.currentThread()? Thread Locals?


Thread Fiber Strand.


API .
— , .


Thread API?



Thread Locals?







Thread:



Fiber:



!
, .



, — - . ?




, : .



?


: , . . — - -. ! .


, , . , , . , .


… , . . — , . — (, ), .





, .


run enter:





, .





VM, freeze. — .





.





, , - , .





, :





:





, , !


.





! . , , - . .





. , . .





, , — , . , VM, VM- thaw. «thaw» «», «», . .


, . . .





.


:





:





, :





:





yield , .





, — , . . , , . — ! , . .


— .


, .





, :





, , :





, :





. C, return barrier:





yield:





C:





, C , . — B, ! , , , — return barrier. , , thaw:





thaw , B:





, , .


B ( , - ). .





, B , - D. .





, freeze, :





, . , .


?


, .



:




OpenJDK. C , fibers.


:


$ hg clone http://hg.openjdk.java.net/loom/loom  
$ cd loom  
$ hg update -r fibers 
$ sh configure   
$ make images

, OpenJDK. , -, - , .





-, C++ GNU . , Windows. , VirtualBox Ubuntu , Cygwin msys64. , msys , Cygwin.


, , , .


- , mercurial extension fsmonitor. , , hg help -e fsmonitor.
~/.hgrc :


[fsmonitor]
mode = on

- . -, cp -R ./loom ./loom-backup.


, . , Java- , .


sh configure - . , Ubuntu, Autoconf (sudo apt-get install autoconf). — OpenJDK Ubuntu, , . Windows , .


, , hg diff --stat -r default:fibers.


, , , .



«, ». «», . «Loom» — « ». Project Loom .


, . , «» , , — , , , — .


, , XIX , .




. -, .


, . IDE .


, , , « », «», « » .


? . .


.

Source: https://habr.com/ru/post/422519/


All Articles