To give some context here, I have been following project loom for some time now. I have read the state of loom. I have done asynchronous programming.
Asynchronous programming (provided by java nio) returns the thread to the thread pool when the task waits and it goes to great lengths to not block threads. And this gives a large performance gain, we can now handle many more request as they are not directly bound by the number of OS threads. But what we lose here, is the context. The same task is now NOT associated with just one thread. All the context is lost once we dissociate tasks from threads. Exception traces do not provide very useful information and debugging is difficult.
In comes project loom with virtual threads
that become the single unit of concurrency. And now you can perform a single task on a single virtual thread
.
It's all fine until now, but the article goes on to state, with project loom:
A simple, synchronous web server will be able to handle many more requests without requiring more hardware.
I don't understand how we get performance benefits with project loom over asynchronous APIs? The asynchrounous APIs
make sure to not keep any thread idle. So, what does project loom do to make it more efficient and performant that asynchronous
APIs?
Let me re-phrase the question. Let's say we have an http server that takes in requests and does some crud operations with a backing persistent database. Say, this http server handles a lot of requests - 100K RPM. Two ways of implementing this:
virtual threads
for every request. If there is an IO, the virtual thread just waits for the task to complete. And then returns the HTTP Response. Basically, there is no pooling business going on for the virtual threads
.Given that the hardware and the throughput remain the same, would any one solution fare better than the other in terms of response times or handling more throughput?
My guess is that there would not be any difference w.r.t performance.
In Java, Virtual threads (JEP-425) are JVM-managed lightweight threads that will help in writing high throughput concurrent applications (throughput means how many units of information a system can process in a given amount of time).
Project Loom is an experimental version of the JDK. It extends Java with virtual threads that allow lightweight concurrency.
Loom is a newer project in the Java/JVM ecosystem (hosted by OpenJDK) that attempts to address limitations in the traditional concurrency model. In particular, Loom offers a lighter alternative to threads along with new language constructs for managing them. Read on for an overview of these important upcoming changes.
Project Loom is an attempt by the OpenJDK community to introduce a lightweight concurrency construct to Java. The prototypes for Loom so far have introduced a change in the JVM as well as the Java library. Although there is no scheduled release for Loom yet, we can access the recent prototypes on Project Loom's wiki.
We don't get benefit over asynchronous API. What we potentially will get is performance similar to asynchronous, but with synchronous code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With