Prev Next

Java / Java 21 Virtual Threads Interview questions

Explain the internal working of the scheduler behind virtual threads?

The default scheduler is a dedicated instance of java.util.concurrent.ForkJoinPool running in FIFO mode (as opposed to the LIFO/work-stealing mode used by the common pool for parallel streams), separate from that common pool entirely.

Each worker thread in this pool is a carrier thread. Runnable virtual threads are represented internally as tasks; idle carriers pull the next runnable virtual thread from the queue, mount its continuation, and execute it until it blocks, yields, or finishes.

When a virtual thread becomes unmounted, it isn't re-queued instantly if it's simply waiting - it re-enters the runnable queue only once its blocking condition is satisfied (I/O ready, timer elapsed, lock acquired). The pool's parallelism, and thus how many virtual threads can truly run in parallel at once, is governed by jdk.virtualThreadScheduler.parallelism. While the scheduler is technically pluggable via an internal, non-public JDK mechanism, replacing it isn't a supported or common practice for application code.

The default virtual thread scheduler is a dedicated instance of:
A virtual thread re-enters the runnable queue when:

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