Java Team’s Efforts on Addressing Virtual Thread Parking and Blocking Issues
Oracle’s introduction of Java’s virtual threads with JDK 21 in September 2023 aimed to simplify concurrent application development but has encountered a significant “pinning” issue related to synchronized methods or statements.
This week, Oracle addressed the virtual thread pinning problem on the Inside Java website, highlighting two primary scenarios: a virtual thread parking within a synchronized method and a virtual thread blocking upon attempting to enter a synchronized method held by another thread’s monitor. In both cases, the underlying carrier or native thread remains occupied, unable to perform other tasks. This issue poses risks to performance, scalability, and can potentially lead to starvation or deadlock, as noted in Oracle’s blog post.
To mitigate these concerns, new early access builds for Java’s Project Loom have revised the object monitor implementation. These changes aim to prevent thread pinning in the identified scenarios, enhancing the reliability and performance of virtual threads in heavily synchronized environments. The Loom development team encourages developers to test these updates extensively, especially in scenarios involving virtual threads and synchronized libraries, and to provide feedback via the Loom mailing list.
Project Loom, part of the OpenJDK initiative, focuses on enhancing Java Virtual Machine (JVM) features and APIs to support lightweight concurrency. Virtual threads, initially previewed in JDK 19 and JDK 20, offer a streamlined approach to handling high-throughput concurrent applications, minimizing the complexities traditionally associated with thread management in Java.
Despite the pinning issue’s challenges, Oracle underscores the positive reception of virtual threads within the Java community and broader ecosystem. The ongoing development efforts and collaborative testing are critical steps towards resolving these issues and further optimizing Java’s concurrency capabilities for future releases.
Oracle detailed the virtual thread pinning issue this week on the Inside Java website. The two most common cases involve a virtual thread parking while in a synchronized method, and a virtual thread blocking when entering a synchronized method, because the object’s associated monitor is held by another thread. In both cases, the carrier or native thread is not released to do other other work. Virtual thread pinning could impact performance and scalability and potentially result in starvation and deadlock, according to the blog post.
New early access builds for Java’s Project Loom introduce changes to the object monitor implementation that do not pin in these two common cases. The Loom team is seeking help from users to test the reliability and performance of these updated object monitors with code that uses virtual threads and with libraries that are heavily synchronized. To report an issue, developers should use the Loom mailing list.