If a thread tries to initialize a class that is already being initialized by another thread, it will block until notified. Since at this blocking point there are native frames on the stack, a virtual thread cannot be unmounted and is pinned to its carrier. Besides harming scalability, this can, in some pathological cases, lead to a deadlock, for example, if the thread executing the class initialization method is blocked waiting for some unmounted virtual thread to run, but all carriers are blocked waiting for that class to be initialized.
As ofJDK-8338383, virtual threads blocked in the VM on ObjectMonitor operations can be preempted. Since synchronization on class initialization is implemented using ObjectLocker, we can reuse the same mechanism to preempt all virtual threads blocked waiting for a class to be initialized along some of the most common initialization paths, in particular, when executing invokestatic, new, getstatic and putstatic from the interpreter.
As of