The Future of Java: Virtual Threads in JDK 21 and Their Impact
The article discusses the addition of virtual threads to JDK 21 and their impact on the future of Java.
Join the DZone community and get the full member experience.
Join For FreeJDK 21, released on September 19, 2023, marks a significant milestone in Java's evolution. It's a long-term support (LTS) release, ensuring stability and support from Oracle for at least eight years. In this release, Java developers are introduced to several new features, including virtual threads, record patterns, pattern matching for switch statements, the foreign function and memory API, and the ZGC garbage collector.
The Significance of Virtual Threads
Among these features, virtual threads stand out as a game-changer in the world of concurrent Java applications. Virtual threads have the potential to revolutionize the way developers write and manage concurrent code. They offer exceptional benefits, making them ideal for high-throughput concurrent applications.
Advantages of Virtual Threads
Virtual threads bring several advantages to the table, differentiating them from traditional platform threads:
- Efficient Memory Usage: Virtual threads consume significantly less memory compared to their traditional counterparts. The JVM intelligently manages memory for virtual threads, enabling reuse and efficient allocation for multiple virtual threads.
- Optimized CPU Usage: Virtual threads are more CPU-efficient. The JVM can schedule multiple virtual threads on the same underlying OS thread, eliminating the overhead of context switching.
- Ease of Use: Virtual threads are easier to work with and manage. The JVM takes care of their lifecycle, removing the need for manual creation and destruction. This simplifies concurrent Java application development and maintenance.
Understanding Types of Threads
To grasp the significance of virtual threads, let's briefly examine three types of threads:
1. OS Threads (Native Threads): These are the fundamental units of execution in modern operating systems. Each OS thread has its own stack, program counter, and register set, managed by the OS kernel.
2. Platform Threads (Traditional Java Threads): Platform threads are built on top of OS threads but managed by the Java Virtual Machine (JVM). They offer more efficient scheduling and resource utilization. A platform thread is an instance of java.lang.Thread implemented in the traditional way, as a thin wrapper around an OS thread.
3. Virtual Threads: Virtual threads are instances of java.lang.Thread is not bound to specific OS threads. Multiple virtual threads can be served by a single OS thread. Virtual threads are even lighter weight and more efficient than traditional threads, and they can be used to improve the performance, scalability, and reliability of concurrent Java applications.
The Challenge of Traditional Threads
Threads are the building blocks of concurrent server applications and have been integral to Java for nearly three decades. Unfortunately, traditional Java threads are expensive to create and maintain. The number of available threads is limited because the JDK implements threads as wrappers around operating system (OS) threads. OS threads are costly, so we cannot have too many of them, which makes the implementation ill-suited to the thread-per-request style limiting the number of concurrent requests that a Java server can handle and thus impacting the server application scalability.
The Shift to Thread-Sharing
Over a period of time, to be able to scale and to utilize hardware to its fullest, java developers transitioned from the thread-per-request style to a thread-sharing style. In thread-sharing, instead of handling a request on one thread from start to finish, the request-handling code returns its thread to a pool when it waits for another I/O operation to complete so that the thread can service other requests.
This fine-grained sharing saves threads but requires asynchronous programming techniques. Asynchronous programming is a complex and error-prone way to improve scalability, and it is not compatible with the thread-per-request style that is common in Java server applications.
Introducing Virtual Threads
Virtual threads offer a compelling solution to preserve the thread-per-request style while enhancing concurrency. They are not tied to specific OS threads, allowing for greater concurrency. Virtual threads consume OS threads only during CPU-bound tasks. When they encounter blocking I/O operations, they are automatically suspended without monopolizing an OS thread. Creating and managing virtual threads is straightforward, eliminating the need for pooling.
Virtual threads are short-lived, perform specific tasks, and are plentiful. Platform threads are heavyweight, long-lived, and may need pooling. Using virtual threads maintains the thread-per-request style while optimizing hardware utilization.
The Future With JDK 21
It's essential to note that with JDK 21, the goal is not to replace traditional thread implementations but to provide an efficient alternative. Developers retain the flexibility to choose between virtual threads and platform threads based on their specific requirements.
Here's a simple example that showcases the power of virtual threads:
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
IntStream.range(0, 10_000).forEach(i -> {
executor.submit(() -> {
Thread.sleep(Duration.ofSeconds(1));
return i;
});
});
// Wait for all tasks to complete.
} // The executor is automatically closed, and it waits for task completion.
In this example, modern hardware efficiently handles 10,000 virtual threads concurrently, executing straightforward code. Behind the scenes, the JDK efficiently manages these virtual threads, optimizing resource utilization.
Conclusion
Java developers have long relied on threads for concurrent programming. With the introduction of virtual threads in JDK 21, achieving high concurrency while preserving a familiar programming style becomes easier. Virtual threads are especially valuable for server applications that handle numerous user requests, offering efficiency and scalability in a changing landscape.
Published at DZone with permission of Roopa Kushtagi. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments