Prev Next

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.

Pinning is problematic because it:
Which construct is a common trigger for pinning in Java 21?

More Related questions...

What is a virtual thread in Java 21? What is Project Loom? What are the types of threads available in Java 21? How do you create a virtual thread? What is the purpose of virtual threads? What is a platform thread? Define a carrier thread? Describe the Thread.Builder API? What is the purpose of Thread.ofVirtual()? How do you check if a thread is virtual? What is a continuation in the context of virtual threads? List the ways to create virtual threads in Java 21? What is the default naming behavior for virtual threads? How do you use Executors.newVirtualThreadPerTaskExecutor()? What is structured concurrency? What is the difference between virtual threads and platform threads? Why is thread pinning a concern with virtual threads? Why do we use virtual threads instead of traditional thread pools? How does the JVM schedule virtual threads? How is a virtual thread mounted onto a carrier thread? When should you use virtual threads over platform threads? When would you choose platform threads over virtual threads? What happens when a virtual thread performs blocking I/O? Why doesn't ThreadLocal work well with virtual threads? What is a ScopedValue and why was it introduced? How does StructuredTaskScope manage a group of virtual threads? Why should you avoid pooling virtual threads? What is the difference between synchronized blocks and ReentrantLock for pinning? How does virtual thread stack size differ from a platform thread's? What happens when you call Thread.sleep() inside a virtual thread? Why is the number of carrier threads limited by default? What is the difference between ExecutorService and StructuredTaskScope? How does virtual thread creation cost compare to platform threads? Why do virtual thread priorities have little practical effect? How do you configure the number of carrier threads used? Explain the lifecycle of a virtual thread? Explain the execution flow when a virtual thread makes a blocking call? Explain the internal working of the scheduler behind virtual threads? How can you optimize an application to fully benefit from virtual threads? How do you troubleshoot thread pinning caused by synchronized blocks? How do you migrate a thread-pool-based application to virtual threads? How do you monitor and debug virtual threads using JFR? How can you detect thread pinning in a production system? Which is better and why: thread pools or virtual threads for I/O-heavy services? Explain how virtual threads interact with native code and JNI calls? What is the difference between virtual threads and reactive programming? What is the difference between virtual threads and Kotlin coroutines or Go goroutines? Explain the internal working of StructuredTaskScope's shutdown behavior? How does the JVM avoid exhausting memory with millions of virtual threads? Explain the execution flow of a structured concurrency task with subtasks failing?
Show more question and Answers...


Comments & Discussions