Ikodes Technology

Preparing for Java Virtual Threads

In this case updateInventory() and updateOrder() will leak and continue to run in the background. That said, Loom was definitely another turning point in our minds. We'll be reconsidering the question of a Reactor 4 when Loom gets closer to GA. for now, JDK19 will see a feature preview. Used for streaming programming and functional programming. Technically, the Reactor Project https://globalcloudteam.com/ has nothing to do with the Loom Project, except some minimal integration which lets one use VirtualThread from Loom to runs async ops using map instead of flatMap. Conceptually, Reactor brings Functional and Reactive Programming into software development and allows focusing on WHAT you do instead of HOW you do that (e.g. functional declarative vs structured imperative).


java loom vs reactive

Cheap to createCreating a platform thread in Java takes time. Currently, techniques such as pooling where threads are created once then reused are strongly encouraged to minimize the time lost in starting them . Virtual threads are supposed to be disposable entities that we create when we need them, it is discouraged to pool them or to reuse them for different tasks. The “Using virtual threads vs. platform threads” section contains example code for using virtual threads. Virtual threads are a new lightweight implementation of a Thread coming to Java. They’re available now as a preview in Java 19, allowing you to trial their usage in conjunction with existing apps.


Let's say that we have a two-lane road , and 10 cars want to use the road at the same time. Naturally, this is not possible, but think about how this situation is currently handled. Traffic lights allow a controlled number of cars onto the road and make the traffic use the road in an orderly fashion. This degradation of performance doesn’t seem to come from virtual threads themselves but from the interactions between Vert.x/Netty and the virtual threads. This was illustrated in the issue that we will now describe. A simple benchmark for JDK Project Loom’s virtual threads.


Bringing virtual threads to reactive REST services


It's just a matter of a single bit when choosing between them. From the operating system's perspective, every time you create a Java thread, you are creating a kernel thread, which is, in some sense you're actually creating a new process. This may actually give you some overview like how heavyweight Java threads actually are.


java loom vs reactive

You just create treads as if it was a very native, very low footprint abstraction, which is not the case right now. The first takeaway is that this may revolutionize the way you work with concurrent code. On the other hand, we can already see that even though the feature wasn't yet released, you have to be aware of the shortcomings.


Because what actually happens is that we created 1 million virtual threads, which are not kernel threads, so we are not spamming our operating system with millions of kernel threads. The only thing these kernel threads are doing is actually just scheduling, or going to sleep, but before they do it, they schedule themselves to be woken up after a certain time. Technically, this particular example could easily be implemented project loom java with just a scheduled ExecutorService, having a bunch of threads and 1 million tasks submitted to that executor. It's just that the API finally allows us to build in a much different, much easier way. Project Loom, which is under active development and has recently been targeted for JDK 19 as a preview feature, has the goal of making it easier to write, debug, and maintain concurrent Java applications.


Why do we need Loom?


It’s the low-level construct that makes virtual threads possible. However, those who want to experiment with it have the option, see listing 3. Today Java is heavily used in backend web applications, serving concurrent requests from users and other applications. In traditional blocking I/O, a thread will block from continuing its execution while waiting for data to be read or written. Due to the heaviness of threads, there is a limit to how many threads an application can have, and thus also a limit to how many concurrent connections the application can handle. This constraint means threads do not scale very well.


Java News Roundup: Loom and Panama Updates, Groovy 4.0, GraalVM 22.0 CE, Jakarta EE RPC - InfoQ.com

Java News Roundup: Loom and Panama Updates, Groovy 4.0, GraalVM 22.0 CE, Jakarta EE RPC.

Posted: Mon, 31 Jan 2022 08:00:00 GMT [source]


My experience is that the actor model approach is subjectively much better. If my experience is anything to go by then Loom will be awesome. When I run this program and hit the program with, say, 100 calls, the JVM thread graph shows a spike as seen below . The command I executed to generate the calls is very primitive, and it adds 100 JVM threads. The second one uses Mutiny reactive streams in a declarative style, it is considered non-blocking due to its signature. This annotation can only be used in conjunction with endpoints annotated with @Blocking or considered blocking because of their signature.


Featured free learning paths


We no longer have to think about this low level abstraction of a thread, we can now simply create a thread every time for every time we have a business use case for that. There is no leaky abstraction of expensive threads because they are no longer expensive. As you can probably tell, it's fairly easy to implement an actor system like Akka using virtual threads, because essentially what you do is you create a new actor, which is backed by a virtual thread. There is no extra level of complexity that arises from the fact that a large number of actors has to share a small number of threads.


java loom vs reactive

Loom and Java in general are prominently devoted to building web applications. Obviously, Java is used in many other areas, and the ideas introduced by Loom may well be useful in these applications. It’s easy to see how massively increasing thread efficiency, and dramatically reducing the resource requirements for handling multiple competing needs, will result in greater throughput for servers. Better handling of requests and responses is a bottom-line win for a whole universe of existing and to-be-built Java applications.


Problems and Limitations - Deep Stack


It will then be possible to perform blocking operations without blocking the platform thread upon which the virtual thread is mounted. This is the reference guide for using virtual threads to write reactive REST services. Please refer to the Writing JSON REST services guides for a lightweight introduction to reactive REST services and to the Writing REST Services with RESTEasy Reactive guide for a detailed presentation.


  • The ability to interrupt a fiber or a virtual thread is crucial when writing concurrent code.
  • Preview releases are available and show what’ll be possible.
  • In the second variant, Thread.ofVirtual() returns a VirtualThreadBuilder whose start() method starts a virtual thread.
  • The virtual threads that are not running at the moment, which is technically called pinned, so they are not pinned to a carrier thread, but they are suspended.
  • You just create treads as if it was a very native, very low footprint abstraction, which is not the case right now.
  • Beyond this very simple example is a wide range of considerations for scheduling.

This means that creating a Java platform thread actually results in creating a "thread-like" structure in your operating system. Virtual threads are a feature available since Java 19 aiming at providing a cheap alternative to platform threads for I/O-bound workloads. They extend the Thread class but are not tied to one specific OS thread.


Project Loom: Revolution in Java Concurrency or Obscure Implementation Detail?


It more or less voluntarily can give up the CPU and other threads may use that CPU. It's much easier when you have multiple CPUs, but most of the time, this is almost always the case, you will never have as many CPUs as many kernel threads are running. This mechanism happens in the operating system level. We also believe that ReactiveX-style APIs remain a powerful way to compose concurrent logic and a natural way for dealing with streams. We see Virtual Threads complementing reactive programming models in removing barriers of blocking I/O while processing infinite streams using Virtual Threads purely remains a challenge. ReactiveX is the right approach for concurrent scenarios in which declarative concurrency (such as scatter-gather) matters.


On my machine, the process hung after 14_625_956 virtual threads but didn’t crash, and as memory became available, it kept going slowly. It’s due to the parked virtual threads being garbage collected, and the JVM is able to create more virtual threads and assign them to the underlying platform thread. It turns out that user threads are actually kernel threads these days. To prove that that's the case, just check, for example, jstack utility that shows you the stack trace of your JVM.


java loom vs reactive

Carrier threadA platform thread used to execute a virtual thread is called a carrier. This isn’t a class distinct from Thread or VirtualThread but rather a functional denomination. Platform threadUp until Java 19, every instance of the Thread class was a platform thread, that is, a wrapper around an OS thread. Creating a platform threads creates an OS thread, blocking a platform thread blocks an OS thread.


Running Spring Applications on Virtual Threads


Many applications make use of data stores, message brokers, and remote services. Running such workloads on Virtual Threads helps reduce the memory footprint compared to Platform Threads and in certain situations, Virtual Threads can increase concurrency. The wiki says Project Loom supports "easy-to-use, high-throughput lightweight concurrency and new programming models on the Java platform." In Java, each thread is mapped to an operating system thread by the JVM . With threads outnumbering the CPU cores, a bunch of CPU time is allocated to schedule the threads on the core. If a thread goes to wait state (e.g., waiting for a database call to respond), the thread will be marked as paused and a separate thread is allocated to the CPU resource.


Many of these projects are aware of the need to improve their synchronized behavior to unleash the full potential of Project Loom. Use of Virtual Threads clearly is not limited to the direct reduction of memory footprints or an increase in concurrency. The introduction of Virtual Threads also prompts a broader revisit of decisions made for a runtime when only Platform Threads were available. And then when it's available, most projects will still be stuck waiting to make the jump from Java 8 to 11 first... Async/await in c# is 80% there - it still invades your whole codebase and you have to be really careful about not blocking, but at least it does not look like ass.



Java requires you to deal with low-level concurrency abstractions, such as ExecutorService, Thread and Future, to realise concurrency. After that, the program no longer needs 10 seconds but only just over one second. It can hardly be faster because every task waits one second. You can reach us directly at or you can also ask us on the forum. Check out these additional resources to learn more about Java, multi-threading, and Project Loom. Cancellation propagation — If the thread running handleOrder() is interrupted before or during the call to join(), both forks are canceled automatically when the thread exits the scope.


In other words, a continuation allows the developer to manipulate the execution flow by calling functions. The Loom docs present the example seen in Listing 3, which provides a good mental picture of how this works. Depending on the benchmark, you might even conclude that Loom’s direct interface to OS threads is not faster than Kotlin. Besides, once Loom is final, also Kotlin coroutines will directly use the same interface.



Instead of dealing with callbacks, observables, or flows, they would rather stick to a sequential list of instructions. I've never done it in Java but it's absolute hell in C++. And it was so bad I'm JavaScript that they eventually added special support in the Chromium debugger to reconstruct stack traces across callback boundaries . That actually helped a lot, but of course it only matters when you're running code in a debugger; trying to read that style of code is still incredibly painful most of the time. Instead of allocating one OS thread per Java thread , Project Loom provides additional schedulers that schedule the multiple lightweight threads on the same OS thread.




Author Bio

Jitendra Yadav

As a senior team lead, Mr. Jitendra Yadav is well versed in all stages of the development cycle for dynamic web projects along with front-end and back-end development in-depth knowledge. He has been working with PHP, Angular CLI, and React JS languages for more than 13 years.

Go Back

Expand Your Digital Horizons With Us

Start a new project or take an existing one to the next level. Get in touch to start
small, scale-up, and go Agile.

    Math Captcha 86 − = 85