J.VI · Java 25 practice question
Consider the following code snippet using Java's virtual threads. Which statement accurately describes the behavior of the `synchronized` block when a virtual thread enters it?
public class SyncExample {
private int counter = 0;
public synchronized void increment() {
// Critical section protected by intrinsic lock
counter++;
}
}
Check answer and explanation
Correct answer: If the virtual thread has to wait for the monitor (lock contention) or blocks inside the `synchronized` block, it unmounts from its carrier so other virtual threads can run on that platform thread.
Explanation
Standard Java synchronization mechanisms (`synchronized` blocks) rely on intrinsic locks, which, in the context of virtual threads, cause the virtual thread to suspend. This suspension frees up the underlying platform thread, allowing the scheduler to run other runnable virtual threads on that same platform thread. The thread doesn't block the *platform* thread in the traditional sense; the virtual thread merely yields its execution context while waiting for the lock release.
Common trap
Option A describes the behavior of traditional, blocking threads when contention is high, but it inaccurately describes the fundamental mechanism when using virtual threads, which aim to keep platform threads busy. Option C is incorrect as intrinsic locks are not automatically replaced.
Source: Project Panama - Virtual Threads (JEP 444) - Although JEP 444 primarily discusses thread scheduling, the behavior of intrinsic locks is governed by standard JVM memory models, which interact with virtual threads as described in concurrency documentation r
Checked against the cited text on 2026-11-27.