88 points · by based2 · 5y ago · 76 comments
As someone that codes Java (using various async libraries) and has dabbled in Go, I really look forward to this, it can't come soon enough.
Tiny link on the right called "State of Loom" seems to have the most background information though, and should be helpful to people like me who had no idea what Loom is/supposed to be:
http://cr.openjdk.java.net/~rpressler/loom/loom/sol1_part1.h...
>... delimited continuations (of some form), and related features, such as explicit tail-call.
I expect internal implementation of continuations in Loom to have non-negligible overhead which may be justified for heavy blocking I/O operations but not for CPU-bound workloads. One benchmark I'd like to see (I may want to create it myself at some point) would run a large number of light threads (fibers) which would exchange data via blocking queues (to mimic goroutines with channels). No I/O, a totally CPU-bound workload with a lot of concurrency and a good potential for scalability. Somehow, I'm not very optimistic about Loom's performance in that context (please tell me if and where I'm wrong).
2. For CPU-bound workloads, virtually any overhead that isn't zero can come to dominate in many situations. The overhead can be made to be zero when the tasks are known in advance and can be inlined. This happens in two situations: generators, where there is one producer that can be inlined together with the consumer, and parallel workloads where all the tasks are the same. I am not aware of any single implementation, or even a single user-facing construct, that gives you the optimal experience both in those situations and in I/O-driven concurrency. If you have only one continuation construct, you must choose whether you want to favour one or the other. C++ favours the former, while Java, like Go, the latter, partly because Java already has a decent parallelism construct, and partly because we believe that the concurrent use-case delivers more value to more people. If parallel use-cases that benefit from continuations and/or generators come in high demand, we can address them later.
Then again, the same would be true in Go. Loom simply brings goroutines to the JVM.
Go might perform better, but that can be due to many other things not specific to the concurrency implementation (like better memory utilization because not every struct is stored on the heap in Go)
Valhalla was first announced in 2014, and Loom in 2017.
Does anyone have some insight on when either of these might finally be ready?
For those not familiar:
Valhalla: introduces "inline types" (structs) and allows using primitive types as generics. Has the potential for quite significant performance improvements because it can reduces all the pointer chasing currently required.
Loom: allows Java to work like Go: code that looks single-threaded but where IO is actually driven by a runtime on a hand full of threads. As in: async without the development and debugging complexity.
So I am not concerned that engineering efforts that allow .jar files from 2000 to keep running in Valhalla/Loom aware JVMs (minus modules issues) to also take equally amount of time.
Generally I think that programs can be structured nicely and have them perform optimally by using traditional threads, backed by idiomatic usage of the java.util.concurrent package and integration of those vanilla threads with the NIO package.
IOW, using NIO doesn't inevitably imply using an async framework (be it bespoke, or official like Loom)?
Personally I've always had my reservations around async, it seems a paradigm that makes things gratuitously harder.
I think that's too high a bar. I think the right question should be "will Loom make it easier to write well performing and maintainable solutions to real problems?" I think by making threads a much cheaper resource the answer will be yes.
For example, the simplest way to write a network server that can handle more than one client at a time is to fork a new thread for every client. Using that model you quickly run into a wall with OS threads in Java, and if you want more performance need to look into using NIO and event loops. With virtual threads you'll be able to go much further with the thread-per-client model.
This seems much debatable. For me the simplest way is creating a thread-per-core pool, and have clients integrate with said pool via a queue. It's a well-understood paradigm that has worked for a long time.
I get the sense of abstraction of having a "thread" per client, but is it really the simplest choice if it needed creating a multi-year project like Loom first?
A precedent that comes to mind is Clojure core.async. It pursues the same sense of abstraction. But does one gain much if the implementation has its own quirks, flaws, etc? Any given abstraction is one bug away from leaking.
So, it's well plausible that the same can happen with Loom - more stuff to learn, more underlying complexity, bigger surface area for bugs.
It all seems too gratuitous too me, so my high-bar question/questioning remains :)
You can see a need for this at least in the Android space: RxJava, Kotlin coroutines, etc.
Also, in a user-managed thread multiplexing system you can't guarantee "fair" scheduling. If you have some DOS bugs in your server you can lock up an entire serving thread. A language-level multiplexing system will not have these same kinds of edge cases.
There are many places this sort of thing becomes helpful. We could also see concurrent processing utilities that could be very helpful. See this for more info on how this could lead to cleaner programs: https://www.youtube.com/watch?v=f6kdp27TYZs
Loom is not a panacea and doesn’t make any new thing possible, it makes certain things easier. And it still will have its limitations.
The situation is quite simple, and governed by Little's law. If your number of threads is greater than the average request rate times the average processing latency (of processing a request entirely on a single thread), then you're all good and it's a well-understood paradigm that's worked for many years. If, on the other hand, that number of threads is smaller than that multiple, then that well-understood paradigm is well-understood to have crashed servers for many years, requiring many more of them even when their utilisation is low. If you're on one side of this, then you don't have a problem to begin with. Loom exists because increasingly more people are on the other side of this problem, where they need multiple servers.
Loom comes from the same place of skepticism of async that you are expressing. Its goal is to make the imperative, blocking style as efficient as the async style while retaining familiar debugging tooling.
Personally, I'm more skeptical of imperative, blocking programming but Loom does seem like it can only be a good thing to have access to on the JVM platform.
If you're running on Linux, this is rather less the case. People see 1MB Xss values and assume that 1MB of 'real' memory is required to spawn a thread. In reality, on a standard linux system with overcommit enabled, stack pages are only consumed as they're written to - the rest is just virtual.
Possibly you're talking about something else, but I commonly see this raised as a concern about creating threads in Java.
1. Once stack memory is committed, it is never uncommitted, and creating a new thread is relatively expensive.
2. Committing virtual memory is done at a page granularity.
These make threads far too costly to serve as a construct for any single concurrent task.
I don't think this is true if you create have approximately 1 thread for each CPU core (which is the ideal scenario in a non-async app - why would you create more threads than CPUs?).
A JVM thread is basically an OS thread. So on a 64-core server you'd have 64 threads, give or take. While a server can handle a few thousands threads quite seamlessly.
As far as using a fixed pool goes: others have replied similarly but by doing that you are completely letting your CPU cores go to waste whenever you are waiting for any underlying IO. For many classes of applications, they will end up topping out at only a few percent CPU usage.
For workloads that involves a lot of IO. If you perform much more than 2-3 IO ops per request, a lot of time spent per request is simply waiting for IO response, meaning your CPUs are mostly idle.
Right, so then how do you structure your program - particularly an I/O-bound program - to use that small pool of threads effectively?
Imagine you're building a REST frontend that queries a couple of datastores and aggregates the results - you get a web request, fire off a request to some coordinator datastore, get a response back from that, fire off a bunch of requests to other datastores based on that, then form the results into some JSON and send them back to the client. If you do blocking requests then you waste most of your threads most of the time (they'll be idle waiting for responses from the datastores).
If you use NIO then that solves that problem, but you've still got to get each thread to actually do the right kind of processing - you want each thread to run an event loop where it checks for returned results from the datastores, matches those up with the requests that they belong to, and does the next step of processing for that request whether it's firing off more requests to other the datastores or composing together the results and sending them back to the original client. And you've got to also handle timing out stale requests etc.
Now you can write a server literally like that, with a global buffers for each possible request type that hold the state that you need to pick up handling that request again. But it's a nightmare to maintain, as you're effectively forced to write unstructured programs where you do a GOTO for each I/O operation - there's no easy way to trace through the handling of a single request or run part of your program for testing.
So people generally prefer to use an async framework that will handle keeping track of each suspended request and what to do next when the result comes back, and multiplex those onto the native threads for you. That could be something where you call an I/O API and pass a callback to run when the result comes back (and the framework takes care of running the callback on some thread when the result is ready, without blocking a thread in the meantime), or something where there's a special operator to say "suspend this function here, package up the rest of it as a continuation, and continue with that continuation when the I/O result comes back". Or, as Loom is doing, it could be something similar that works completely invisibly whenever the programmer calls particular functions.
There are tradeoffs to all these approaches to async, but any of them is going to perform a lot better than the naive approach of just using a native thread for each request, and they're all (hopefully) going to be more maintainable than manually writing an event loop that does the right thing each time.
The trouble is that threads are heavyweight, particularly in context switching. So the user has to work around it by explicitly creating async functions, task schedulers, futures and so on, to avoid having to block a crucial thread (as in GUIs or network event loops).
Languages like go and erlang help you avoid that by having the entire ecosystem (libraries included) give you permission to write blocking code, and that under the hood, they would take care of fast context switching and async I/O. That is what loom hopes to do. Ideally in this world, the ordinary developer would not have to use nio at all ... simple blocking APIs should suffice.
That said, Loom is a neat tool and I expect new, more convenient patterns will pop up from Loom.
I'll be interested to see how Java tackles OS thread problems after Loom. We'll still live in a world where most(all?) GUIs frameworks revolve around single thread concurrency and GPU access is still single threaded. I don't see how the implicit threading of loom is going to work for that, and it also seems like a line in the sand has been drawn in the face of async/await. We'll just have to see what comes of it.
Rather virtual threads get created by default and an explicit call, or thread factory can be used to get the value back.
At least that is how I remember it.
This is one of the major driving forces behind Loom being done so differently to how they’re done elsewhere.
Async became so popular due to sync code not scaling for large concurrent use cases. Loom is about leaving sync application code be, and optimising the underlying runtime instead.
The implicit threading of Loom works well for things like transparently dealing with IO without blocking an OS thread.
For things like posting back to a UI thread, you'll still need explicit handling not unlike async/await. It seems like Java is taking a scoped approach. https://wiki.openjdk.java.net/display/loom/Structured+Concur...
By doing it this way, the entire JVM ecosystem get's the benefits of async/await without changing the Java language. It's also easier for developers to learn (do what you're used to with threads, just use this .virtualThreadPool function instead and be extravagant with the number of threads in the pool).
> International use restrictions
> Due to limited intellectual property protection and enforcement in certain countries, the source code may only be distributed to an authorized list of countries. You will not be able to access the source code if you are downloading from a country that is not on this list. We are continuously reviewing this list for addition of other countries.
Maybe it's just that Oracle that doesn't want to convey the software to certain nationals, but anyone else with a valid GPL license may convey it?
Besides, they either own the code, licensed said code by a contributor or another software maker to Oracle on a perpetual, sublicensable terms or used Expat-licensed (MIT-licensed) code. All of these does not restrict Oracle from assigning any copyright conditions apart from the required attributions on Expat-licensed code. It could be closed-source and they will still be fine.
You can’t forbid yourself from applying whatever terms you want on your own code.