Java / Java 21 Virtual Threads Interview questions
Why is thread pinning a concern with virtual threads?
Pinning happens when a virtual thread cannot be unmounted from its carrier while it's blocked, forcing the carrier thread to sit idle instead of running other virtual threads. This defeats the scalability benefit virtual threads are meant to provide.
The two main triggers in Java 21 are blocking inside a synchronized block or method, and executing native code (JNI). If enough virtual threads pin at once, the limited carrier pool can be fully occupied, starving every other virtual thread waiting to run - even ones unrelated to the pinning code.
This is why high-throughput services are advised to replace synchronized with java.util.concurrent.locks.ReentrantLock around code paths that also block.
More Related questions...