Virtual Threads: New Foundations for High-Scale Java Applications

When a request is handled by the web server it is using a Java Thread and that thread is tied to an operating system thread. When you make a call to a blocking operation like reading or persisting to a database that thread is blocked from doing anything else until the request is fulfilled. This is a perfect example of the mismatch between the reactive stack and the virtual threads. The fundamental hypothesis are completely different and result in different optimizations. Netty expects a system using few event-loops (as many event-loops as CPU cores by default in Quarkus), but it gets hundreds of thousands of threads.

java virtual threads example

We will define what virtual threads are and how they differ from normal threads. Additionally, we will discuss the benefits of virtual threads over traditional concurrency approaches and provide code examples to illustrate the differences between the two. By the end of this article, you should have a better understanding of Java Virtual Threads and how they can be used to write more efficient and scalable concurrent code in Java. It has potential to improve an applications availability, throughput and code quality on top of reducing memory consumption.

more stack exchange communities

The creation of platform threads is very expensive, therefore the need to use thread pools, and we are always limited by the number of processing units available in our host. Here is an example which creates 10,000 virtual threads; however, the JDK runs the code on perhaps only one OS thread. If we were using 10,000 platform threads instead, the program might crash, depending on the hardware available.

For example, if a request takes two seconds and we limit the thread pool to 100 threads, then a maximum of 50 requests per second could be answered. However, the CPU would be far from being utilized since it would spend most of its time waiting for responses from the external services, even if several threads are served per CPU core. Virtual threads are suitable for running tasks that spend most of the time blocked, often waiting for I/O operations to complete. However, they aren’t intended for long-running CPU-intensive operations.

Migrating from Reactive to virtual threads

This is because virtual threads have better handling of IO-bound tasks, and can avoid the overhead of creating and destroying OS-level threads. Enable existing code that uses thejava.lang.ThreadAPI to adopt virtual threads with minimal change. Again, virtual threads beat platform threads with a considerable difference.

java virtual threads example

It also removes the experimental AOT and JIT compilers, and introduces support for Sealed Classes and Records. These changes provide developers with more flexibility and control, making it easier to write efficient and secure Java applications. After creating the virtual thread, we use the CompletableFuture.get() method to wait for the task to complete and retrieve its result. Note that because the task is executed asynchronously, the main thread is free to perform other work while the task is running. If your application is creating new threads frequently , switching to virtual threads can improve your application’s response time. If your application either have lot of threads or large stack size (i.e. -Xss), then switching to virtual threads would reduce your application’s memory consumption.

Threads

A new capability, can_support_virtual_threads, gives agents finer control over thread start and end events for virtual threads. These operations will cause the virtual thread to mount and unmount multiple times, typically once for each https://globalcloudteam.com/ call to get() and possibly multiple times in the course of performing I/O in send(…). In addition, from the perspective of Java code, the fact that a virtual thread and its carrier temporarily share an OS thread is invisible.

java virtual threads example

Like the factory method, you can use the builder method, which is much more convenient as you can create started or unstarted threads. I’m a freelance software developer with more than two decades of experience in scalable Java enterprise applications. My focus is on optimizing complex algorithms and on advanced topics such as concurrency, the Java memory model, and garbage collection. Here on HappyCoders.eu, I want to help you become a better Java programmer.Read more about me here.

Virtual Threads in Java (Project Loom)

Unfortunately, as programs scale, this approach is on a collision course with the memory characteristics of platform threads. The stacks of virtual threads are stored in Java’s garbage-collected heap as stack chunk objects. The stacks grow and shrink as the application runs, both to be memory-efficient and to accommodate stacks of depth up to the JVM’s configured platform thread stack size.

java virtual threads example