Project Loom: Understand the new Java concurrency model

Java performance issues are difficult to track down or predict. And, of course, it can be heavily visual, allowing you to
interact with the database using diagrams, visually compose
queries, explore the data, generate random data, import data or
build HTML5 database reports. The way it does all of that is by using a design model, a
database-independent image of the schema, which can be shared in a
team using GIT and compared or deployed on to any database. A good way to go is, naturally, a dedicated profiler that
actually understands the ins and outs of MySQL. AI is all the rage these days, but for very good reason.

Reasons for Using Java Project Loom

But with file access, there is no async IO (well, except for io_uring in new kernels). However, operating systems also allow you to put sockets into non-blocking mode, which return immediately when there is no data available. And then it’s your responsibility to check back again later, to find out if there is any new data to be read.

« Sequenced Collections »: an upcoming feature of Java 21 to handle a sequence of elements in a better way.

Fourth level architecture to have better understanding. With Loom, we write synchronous code, and let someone else decide what to do when blocked. And debugging is indeed painful, and if one of the intermediary stages results with an exception, the control-flow goes hay-wire, resulting in further code to handle it.

In vast majority of use cases you will set the data once and only read that data throughout the request context, but that cannot be enforced easily, meaning data.set(« data ») can be called anytime, anyplace and any number of times. This is the first design difference that Scoped Values made — they make the data immutable, set only once and are read-only afterwards. In this scenario (Project Loom & Virtual Threads), the benchmark is comparing the number of OS threads required for some logic in Java. In one side, it’s using Java Platform Threads to run 1000 tasks, and in the other it’s using Virtual Threads to run also 1000 tasks.

On Repeat: Java’s Record Feature Plays a New Tune!

There wasn’t any network IO involved, but that shouldn’t have impacted the results. So, if your task’s code does not block, do not bother with virtual threads. Most tasks in most apps are often waiting for users, storage, networks, attached devices, etc. An example of a rare task that might not block is something that is CPU-bound like video-encoding/decoding, scientific data analysis, or some kind of intense number-crunching.

  • However, there’s a whole bunch of APIs, most importantly, the file API.
  • An important note about Loom’s fibers is that whatever changes are required to the entire Java system, they are not to break existing code.
  • Stay tuned, we will soon be sharing another article on other new features of Java 19.
  • The primary goal of Project Loom is to make concurrency more accessible, efficient, and developer-friendly.
  • It executes the task from its head, and any idle thread does not block while waiting for the task.

OS threads are at the core of Java’s concurrency model and have a very mature ecosystem around them, but they also come with some drawbacks and are expensive computationally. Let’s look at the two most common use cases for concurrency and the drawbacks of the current Java concurrency model in these cases. Virtual threads are wrapped upon platform threads, so you may consider them an illusion that JVM provides, the whole idea is to make lifecycle of threads to CPU bound operations.

Fibers: Virtual threads in Java

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. The Loom project started in 2017 and has undergone many changes and proposals. Virtual threads were initially called fibers, but later on they were renamed to avoid confusion. Today with Java 19 getting closer to release, the project has delivered the two features discussed above. Hence the path to stabilization of the features should be more precise.

Reasons for Using Java Project Loom

Java’s threading model, while powerful, has often been considered too complex and error-prone for everyday use. Enter Project Loom, a paradigm-shifting initiative designed to transform the way Java handles concurrency. It helped me think of virtual threads as tasks, that will eventually run on a real thread⟨™) (called carrier thread) AND that need the underlying native calls to do the heavy non-blocking lifting. With Project Loom, we will have at least one more such option to choose from. In the preview build of Java, call the new Executors.newVirtualThreadPerTaskExecutor() to get an executor service backed by virtual threads.

Why Spring is Faster Than Vert.x?

Virtual threads, as the primary part of the Project loom, are currently targeted to be included in JDK 19 as a preview feature. If it gets the expected response, the preview status of the virtual threads will then be removed by the time of the release of JDK21. To cater to these issues, the asynchronous non-blocking I/O were used. The use of asynchronous I/O allows a single thread to handle multiple concurrent connections, but it would require a rather complex code to be written to execute that.

Oracle JDK 21 LTS Release: More Features from Loom, Panama … – ADT Magazine

Oracle JDK 21 LTS Release: More Features from Loom, Panama ….

Posted: Wed, 27 Sep 2023 07:00:00 GMT [source]

They just were not always used, given the simplicity/ease of relying on the limits/bottlenecks of a limited number of platform threads. Each one is a stage, and the resultant CompletablFuture is returned back to the web-framework. And while pooling does immensely help because you are not paying a hefty price of creating a new thread every time, it does not increase the total number of available threads. For Platform Threads, it created around 1000 Platform Threads during the test, using a 2-3% of CPU, and 150 Mb of memory.


Deepu is a polyglot developer, Java Champion, and OSS aficionado. He co-leads JHipster and created the JDL Studio and KDash. He is also an international speaker and published author. For these situations, we would have to carefully write workarounds and failsafe, putting all the burden on the developer. Structured concurrency will be an incubator feature in Java 19.

Reasons for Using Java Project Loom

There will be a preview reach release with 6 new features delivering either incubators or preview features. Virtual threads under Project Loom also require minimal changes to code, which will encourage its adoption in existing Java libraries, java project loom Hellberg said. 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.

Project Loom: virtual threads in Java

When these features are production ready, it will be a big deal for libraries and frameworks that use threads or parallelism. Library authors will see huge performance and scalability improvements while simplifying the codebase and making it more maintainable. Most Java projects using thread pools and platform threads will benefit from switching to virtual threads.