Prev Next

Java / Java 21 Virtual Threads Interview questions

Explain the internal working of StructuredTaskScope's shutdown behavior?

Policies like ShutdownOnFailure and ShutdownOnSuccess watch the subtasks forked within a scope for a triggering condition - the first failure, or the first success, respectively.

Once that condition fires, the scope calls shutdown() internally, which interrupts every remaining forked virtual thread that hasn't finished yet. This is cooperative cancellation: a subtask must itself be interruption-aware (checking Thread.interrupted() or using interruptible blocking calls) to actually stop promptly rather than ignoring the interrupt.

The blocked scope.join() call in the parent then returns, after which methods like throwIfFailed() surface the captured exception (wrapped appropriately) to the caller. Finally, the implicit close() from try-with-resources verifies that no subtask thread is still running past the scope boundary - if one were, close() would wait for or forcibly account for it, upholding the guarantee that nothing outlives the scope.

When ShutdownOnFailure's trigger condition fires, remaining subtasks are:
StructuredTaskScope.close() ultimately guarantees:

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