Q67: What is Thread Pinning in Virtual Threads and what causes it?
Virtual Threads unmount from Carrier Threads when performing blocking I/O. Thread Pinning occurs when a Virtual Thread CANNOT unmount from its OS Carrier Thread during a blocking call, tying up OS thread capacity.
- Causes: Executing blocking operations inside
synchronizedblocks or methods, or calling native JNI methods. - Fix: Replace
synchronizedblocks withReentrantLock.
Key Takeaways
- Pinning occurs when a Virtual Thread cannot unmount from its OS Carrier Thread.
- Avoid
synchronizedinside high-concurrency Virtual Thread paths; useReentrantLock.
Knowledge Check
How can developers prevent Thread Pinning inside Virtual Thread execution paths?