Blog Logo
TAGS

JEP 444: Virtual Threads Arrive in JDK 21, Ushering a New Era of Concurrency

JEP 444, Virtual Threads, was promoted from Proposed to Target to Targeted status for JDK 21. This feature provides virtual threads, lightweight threads that dramatically reduce the effort of writing, maintaining, and observing high-throughput concurrent applications on the Java platform. This JEP intends to finalize this feature based on feedback from the previous two rounds of preview: JEP 436, Virtual Threads (Second Preview), delivered in JDK 20; and JEP 425, Virtual Threads (Preview), delivered in JDK 19. With this JEP, Java now has two types of threads: traditional threads, also called platform threads, and virtual threads. Platform threads are a one-to-one wrapper over operating system threads, while virtual threads are lightweight implementations provided by the JDK that can run many virtual threads on the same OS thread. Virtual threads offer a more efficient alternative to platform threads, allowing developers to handle a large number of tasks with significantly lower overhead. These threads offer compatibility with existing Java code and a seamless migration path to benefit from enhanced performance and resource utilization. The java.util.concurrent package now includes support for virtual threads. The LockSupport API has been updated to gracefully park and unpark virtual threads, enabling APIs that use LockSupport, such as Locks, Semaphores, and blocking queues, to function seamlessly with virtual threads. The Executors.newThreadPerTaskExecutor(ThreadFactory) and Executors.newVirtualThreadPerTaskExecutor() methods provide an ExecutorService that creates a new thread for each task, facilitating the migration and interoperability with existing code that uses thread pools and ExecutorService. Networking APIs in the java.net and java.nio.channels packages now support virtual threads, enhancing efficiency in concurrent applications. Blocking operations on a virtual thread free up the underlying platform thread, while I/O methods in the Socket, ServerSocket, and DatagramSocket classes have been updated to support virtual threads.