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 synchronized blocks or methods, or calling native JNI methods.
  • Fix: Replace synchronized blocks with ReentrantLock.
Key Takeaways
  • Pinning occurs when a Virtual Thread cannot unmount from its OS Carrier Thread.
  • Avoid synchronized inside high-concurrency Virtual Thread paths; use ReentrantLock.
Knowledge Check

How can developers prevent Thread Pinning inside Virtual Thread execution paths?