Java 24 brings significant improvements to virtual threads, with JEP 491 marking a key step. Virtual threads are a revolutionary feature introduced to the Java platform in recent years, designed to improve the scalability and efficiency of multithreaded applications. However, thread pinning during synchronization has kept them from reaching their full potential. By addressing these limitations, Java 24 gives developers new ways to build high-performance, highly concurrent applications.
Virtual Threads: Recap and How They Work
Virtual threads are lightweight threads introduced by Project Loom and scheduled by the JVM rather than the operating system. Compared with traditional platform threads, they consume fewer resources, allowing Java applications to create large numbers of threads for high concurrency.
However, blocking operations such as I/O or synchronization can pin a virtual thread to a platform thread, undermining their advantages. Java 24 adjusts the synchronization mechanism to address this.
Major Improvement: Eliminating Pinning in Synchronization
One of the highlights of Java 24 is that virtual threads are no longer pinned during synchronization. Previously, synchronization constructs such as synchronized blocks or methods required the JVM to bind a virtual thread to a platform thread to manage locks. This led to contention and reduced the scalability of virtual threads.
With JEP 491, the JVM can now recognize virtual threads and avoid unnecessary pinning. This allows virtual threads to suspend and resume without occupying a platform thread, so tens of thousands of virtual threads can coexist efficiently.
Remaining Challenges: Cases Where Pinning Can Still Occur
Although Java 24 greatly reduces pinning, virtual threads can still be pinned in the following cases:
- Native code calls: When a virtual thread interacts with native code or JNI (Java Native Interface), pinning may still be required. This typically happens during class loading or when accessing native libraries.
- Certain blocking operations: Some legacy I/O operations or unoptimized third-party libraries can still cause threads to be pinned.
These cases are relatively rare, but they show that fully decoupling virtual threads from platform threads remains a challenge.
Observing Thread Pinning with JDK Flight Recorder
To help developers identify and resolve thread pinning issues, Java 24 improves observability through JDK Flight Recorder (JFR). JFR provides detailed records of thread activity, including instances where virtual threads are pinned and the reasons behind them.
This provides valuable data for diagnosing performance bottlenecks in complex applications and helps developers optimize their use of virtual threads.
File I/O and Scalability: An Unsolved Problem
Although synchronization-related pinning has been addressed, file I/O operations remain a major scalability challenge. File I/O often involves thread capture—pinning a virtual thread to a platform thread during a blocking file operation.
Solutions such as Linux's io_uring have been proposed that can eliminate the need for blocking I/O entirely. However, these approaches add system complexity and make portability harder, so their adoption on the Java platform will take time.
For now, developers are advised to work around these issues by using non-blocking I/O (such as java.nio) or asynchronous frameworks.
Conclusion: The Road to Java Concurrency Excellence
Java 24 is an important milestone in the evolution of virtual threads, resolving one of the key bottlenecks to their adoption. By eliminating pinning during synchronization, JEP 491 improves the scalability and efficiency of highly concurrent Java applications.
As JDK 24 enters the Ramp-Down phase, the focus will shift to finalizing and stabilizing features. Looking ahead, improvements in I/O handling, native code interaction, and full ecosystem support for virtual threads will further unlock their potential.
The journey to fully leveraging virtual threads is still ongoing, but Java 24 has moved us significantly forward and made lightweight, highly concurrent applications a reality.