Project Loom: Lightweight Java threads

Even more interestingly, from the kernel point of view, there is no such thing as a thread versus process. This is just a basic unit of scheduling in the operating system. The only difference project loom java between them is just a single flag, when you’re creating a thread rather than a process. When you’re creating a new thread, it shares the same memory with the parent thread.

project loom java

Given its a VM level abstraction, rather than just code level (like what we have been doing till now with CompletableFuture etc), It lets one implement asynchronous behavior but with reduce boiler plate. To give some context here, I have been following Project Loom for some time now. With this new version, the threads look much better (see below). By default, the Fiber uses the ForkJoinPool scheduler, and, although the graphs are shown at a different scale, you can see that the number of JVM threads is much lower here compared to the one thread per task model. This resulted in hitting the green spot that we aimed for in the graph shown earlier. The scheduler allocates the thread to a CPU core to get it executed.

Embracing Virtual Threads

Much of this complexity is hidden from the user to make this code look simpler. Still, a different mindset was required for using asynchronous I/O as hiding the complexity cannot be a permanent solution and would also restrict users from any modifications. A thread supports the concurrent execution of instructions in modern high-level programming languages and operating systems.

If a virtual thread is blocked due to a delay by an I/O task, it still won’t block the thread as the virtual threads can be managed by the application instead of the operating system. This could easily eliminate scalability issues due to blocking I/O. When a socket is not ready for I/O it is registered with a background multiplexer thread. The virtual thread is then unpacked when the socket is ready for I/O. 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.

Synchronous Vs. Asynchronous

Our focus currently is to make sure that you are enabled to begin experimenting on your own. If you encounter specific issues in your own early experiments with Virtual Threads, please report them to the corresponding project. 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. Already, Java and its primary server-side competitor Node.js are neck and neck in performance. An order of magnitude boost to Java performance in typical web app use cases could alter the landscape for years to come.

  • User threads are created by the JVM every time you say newthread.start.
  • But first, let’s see how the current one task per thread model works.
  • Most Java projects using thread pools and platform threads will benefit from switching to virtual threads.
  • To cater to that, the project Loom also aims to add lightweight stack retrieval while resuming the continuation.

We will be discussing the prominent parts of the model such as the virtual threads, Scheduler, Fiber class and Continuations. The continuations used in the virtual thread implementation override onPinned so that if a virtual thread attempts to park while its continuation is pinned (see above), it will block the underlying carrier thread. You can use this guide to understand what Java’s Project loom is all about and how its virtual threads (also called ‘fibers’) work under the hood. Assumptions leading to the asynchronous Servlet API are subject to be invalidated with the introduction of Virtual Threads.

Inspired by this content? Write for InfoQ.

However, if I now run the continuation, so if I call run on that object, I will go into foo function, and it will continue running. It runs the first line, and then goes to bar method, it goes to bar function, it continues running. Then on line 16, something really exciting and interesting happens. The function bar voluntarily says it would like to suspend itself.

In addition, let’s not forget than Loom is not yer ready for production, so there is still margin to improve the behavior. When you run this test with many threads, be prepared; it can be a bit hard on your PC, and you might need a reboot. Fibry is my Actor System, designed to be small, flexible, simple-to-use, and, of course, take advantage of Loom. It works with any version of Java, starting from Java 8 onwards, and it has no dependencies, except requires Loom to use fibers. While not everything works, Loom can now be used with network operations.

Get support

A working application on how to implement the Record Patterns and Pattern Matching for switch APIs may be found in this GitHub repository, java-19 folder, by Wesley Egberto, Java technical lead at Global Points. JEP 405 and JEP 427 fall under the auspices of Project Amber, a project designed to explore and incubate smaller Java language features to improve productivity. If they do, they could help the mature Java language keep up with newer web applications, said Marcus Hellberg, vice president of developer relations at Vaadin, makers of a Java web development framework based in Finland. However, forget about automagically scaling up to a million of private threads in real-life scenarios without knowing what you are doing.

project loom java

And now you can perform a single task on a single virtual thread. The wiki says Project Loom supports “easy-to-use, high-throughput lightweight concurrency and new programming models on the Java platform.” Again we see that virtual threads are generally more performant, with the difference being most pronounced at low concurrency and when concurrency exceeds the number of processor cores available to the test. JEP 428, Structured Concurrency (Incubator), proposes to simplify multithreaded programming by introducing a library to treat multiple tasks running in different threads as a single unit of work. This can streamline error handling and cancellation, improve reliability, and enhance observability. The good news for early adopters and Java enthusiasts is that Java virtual threads libraries are already included in the latest early access builds of JDK 19.

Revision of Concurrency Utilities

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. This model is fairly easy to understand in simple cases, and Java offers a wealth of support for dealing with it. Check out these additional resources to learn more about Java, multi-threading, and Project Loom.

Further, each thread has some memory allocated to it, and only a limited number of threads can be handled by the operating system. At high levels of concurrency when there were more concurrent tasks than processor cores available, the virtual thread executor again showed increased performance. This was more noticeable in the tests using smaller response bodies. Project Loom features that reached their second preview and incubation stage, respectively, in Java 20 included virtual threads and structured concurrency.

Comparing Fibers and Threads

Structured concurrency can help simplify the multi-threading or parallel processing use cases and make them less fragile and more maintainable. First, let’s see how many platform threads vs. virtual threads we can create on a machine. My machine is Intel Core i H with 8 cores, 16 threads, and 64GB RAM running Fedora 36. Virtual threads are lightweight threads that are not tied to OS threads but are managed by the JVM. They are suitable for thread-per-request programming styles without having the limitations of OS threads. You can create millions of virtual threads without affecting throughput.

Leave a Reply