Java / Java 21 Coding Standards Interview Questions
What is the difference between synchronized blocks and virtual-thread-friendly designs?
| synchronized block | Virtual-thread-friendly design |
| Historically pins the carrier thread for the block's duration when used on a virtual thread. | Uses java.util.concurrent locks, which release the carrier thread while waiting. |
| A pinned carrier thread cannot run any other virtual thread meanwhile. | Carrier threads stay available to run other virtual threads while one waits. |
| Fine for short, rarely-contended critical sections. | Preferred for locks that are held for longer or under real contention. |
The coding standard is not "never use synchronized" - short, uncontended critical sections are still fine - but rather to prefer java.util.concurrent.locks.ReentrantLock or higher-level concurrency utilities for critical sections that are long-running or frequently contended, specifically in code paths that run on virtual threads.
JFR's virtual thread pinning events are the standard way to actually verify the impact rather than guessing: they identify which specific synchronized blocks are pinning carrier threads in practice, so the fix is targeted at the blocks that matter instead of a blanket rewrite of every lock in the codebase.
More Related questions...