Prev Next

Java / Java 21 Virtual Threads Interview questions

Why doesn't ThreadLocal work well with virtual threads?

ThreadLocal was designed under the assumption that threads are relatively few and long-lived, often reused via a pool, so caching a value per thread was cheap and effective.

With virtual threads, you may have millions of short-lived instances, each potentially allocating its own ThreadLocal entry. That multiplies memory usage and defeats the original caching intent, since there's no pooled thread to actually reuse the cached value on.

InheritableThreadLocal is worse still, because it must copy values down to every child thread, adding overhead at exactly the scale virtual threads are meant to avoid. Java 21's answer to this is ScopedValue.

ThreadLocal assumes threads are:
A key problem with ThreadLocal at virtual-thread scale is:

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