Software development

Project Loom: Understand the new Java concurrency model

If you think about it, we do have a very old class like RestTemplate, which is like this old school blocking HTTP client. With Project Loom, technically, you can start using RestTemplate again, and you can use it to, very efficiently, run multiple concurrent connections. Because RestTemplate underneath uses HTTP client from Apache, which uses sockets, and sockets are rewritten so that every time you block, or wait for reading or writing data, you are actually suspending your virtual thread. It seems like RestTemplate or any other blocking API is exciting again.

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. If I want to demonstrate the performance advantage of Virtual Threads over Platform Threads, I need to find a better use case.

Releases

Author, trainer, conference speaker, technical reviewer, runner. Claims that code not tested automatically is not a feature but just a rumor. Spring Runtime offers support and binaries for OpenJDK™, Spring, and Apache Tomcat® in one simple subscription.

java project loom

Essentially, a continuation is a piece of code that can suspend itself at any moment in time and then it can be resumed later on, typically on a different thread. You can freeze your piece of code, and then you can unlock it, or you can unhibernate it, you can wake it up on a different moment in time, and preferably even on a different thread. This is a software construct that’s built into the JVM, or that will be built into the JVM. Project Loom allows the use of pluggable schedulers with fiber class. In asynchronous mode, ForkJoinPool is used as the default scheduler.

Reactive programming

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, https://www.globalcloudteam.com/ and 64GB RAM running Fedora 36. Virtual threads are lightweight threads that are not tied to OS threads but are managed by the JVM.

This is a user thread, but there’s also the concept of a kernel thread. A kernel thread is something that is actually scheduled by your operating system. I will stick to Linux, because that’s probably what you use in production. For example, when a kernel thread runs for too long, it will be preempted so that other threads can take over. 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.

Write A Comment Cancel Reply

Virtual threads could be a no-brainer replacement for all use cases where you use thread pools today. This will increase performance and scalability in most cases based on the benchmarks out there. Structured concurrency can help simplify the multi-threading or parallel processing use cases and make them less fragile and more maintainable. So in a thread-per-request model, the throughput will be limited by the number of OS threads available, which depends on the number of physical cores/threads available on the hardware.

java project loom

Running such workloads on Virtual Threads helps reduce the memory footprint compared to Platform Threads and in certain situations, Virtual Threads can increase concurrency. „Before Loom, we had two options, neither of which was really good,“ said Aurelio Garcia-Ribeyro, senior director of project management at Oracle, in a presentation at the Oracle DevLive conference this week. This article discusses the problems in Java’s current concurrency model and how the Java project Loom aims to change them. We also explored the tasks and schedulers in threads and how Fibers Class and pluggable user-mode schedulers can be an excellent alternative for traditional threads in Java. The Loom project started in 2017 and has undergone many changes and proposals.

Lower-level async with continuations

Rather than showing a single Java process, you see all Java threads in the output. More importantly, you can actually see, what is the amount of CPU consumed by each and every of these threads? Does it mean that Linux has some special support for Java? Because it turns out that not only user threads on your JVM are seen as kernel threads by your operating system. On newer Java versions, even thread names are visible to your Linux operating system. Even more interestingly, from the kernel point of view, there is no such thing as a thread versus process.

  • Let’s say that we have a two-lane road (two core of a CPU), and 10 cars want to use the road at the same time.
  • Many applications make use of data stores, message brokers, and remote services.
  • In other words, a continuation allows the developer to manipulate the execution flow by calling functions.
  • 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.
  • They are suitable for thread-per-request programming styles without having the limitations of OS threads.
  • With the H switch, it actually shows individual threads rather than processes.

The new virtual threads in Java 19 will be pretty easy to use. Compare the below with Golang’s goroutines or Kotlin’s coroutines. Each one is a stage, and the resultant CompletablFuture is returned back to the web-framework. Java has long offered such throttling mechanisms. They just java project loom were not always used, given the simplicity/ease of relying on the limits/bottlenecks of a limited number of platform threads. By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

more stack exchange communities

Once we reach the last line, it will wait for all images to download. Once again, confront that with your typical code, where you would have to create a thread pool, make sure it’s fine-tuned. Notice that with a traditional thread pool, all you had to do was essentially just make sure that your thread pool is not too big, like 100 threads, 200 threads, 500, whatever. You cannot download more than 100 images at once, if you have just 100 threads in your standard thread pool. The structured concurrency API is also designed to preserve order in multi-threaded environments by treating multiple tasks running in individual threads as a single logical unit of work.

For instance, Thread.ofVirtual() method that returns a builder to start a virtual thread or to create a ThreadFactory. Similarly, the Executors.newVirtualThreadPerTaskExecutor() method has also been added, which can be used to create an ExecutorService that uses virtual threads. You can use these features by adding –enable-preview JVM argument during compilation and execution like in any other preview feature. 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. When these features are production ready, it should not affect regular Java developers much, as these developers may be using libraries for concurrency use cases. But it can be a big deal in those rare scenarios where you are doing a lot of multi-threading without using libraries.

Revision of Programming Models

Internally, it was doing all this back and forth switching between threads, also known as context switching, it was doing it for ourselves. This would be accomplished via virtual threads, delimited continuations and tail calls. One solution is making use of reactive programming. So, if a CPU has four cores, there may be multiple event loops but not exceeding to the number of CPU cores. This approach resolves the problem of context switching but introduces lots of complexity in the program itself.