Prev Next

Java / Java Concurrency and Multi Threading in Java 17 and Java 21 Interview questions

Explain the lifecycle of a virtual thread in Java 21?

A virtual thread's lifecycle looks like an ordinary thread's from the outside, it goes through NEW, RUNNABLE, and TERMINATED, plus WAITING/TIMED_WAITING/BLOCKED as needed, but the mechanics underneath are different.

flowchart LR
  A[Created via Thread.ofVirtual] --> B[Scheduled onto a carrier platform thread]
  B --> C{Blocking operation?}
  C -- Yes, e.g. I/O --> D[Unmounted from carrier; carrier freed for other virtual threads]
  D --> E[Operation completes]
  E --> B
  C -- No --> F[Runs to completion or yields]
  F --> G[Terminated]

When created and started, a virtual thread is handed to the JVM's built-in scheduler, which is itself a ForkJoinPool, and mounted onto an available carrier (an ordinary platform thread). While it's doing CPU work or calling most JDK-supported blocking operations, it stays mounted.

When it hits a blocking point the JVM knows how to handle non-blockingly underneath, such as socket I/O, Thread.sleep(), or blocking on a java.util.concurrent lock, the JVM unmounts it, storing its stack in the heap and freeing the carrier thread to run a different virtual thread. Once the operation completes, the virtual thread is remounted on some available carrier, not necessarily the same one, and continues.

This mount/unmount cycle, invisible to application code, is what lets a small pool of carrier threads support enormous numbers of concurrently blocked virtual threads. When the virtual thread's task returns, it terminates and its resources are reclaimed like any object; there is no OS thread teardown cost since none was created.

What happens when a mounted virtual thread hits a JDK-supported blocking operation?
What scheduler manages virtual threads under the hood?

More Related questions...

What is a thread in Java? What is multithreading in Java? What are the different states of a thread in Java? How do you create a thread in Java? What is the Runnable interface used for? What is a daemon thread in Java? What is thread priority in Java? What is the purpose of the synchronized keyword? What is the purpose of the volatile keyword? What are the types of locks available in java.util.concurrent.locks? Define a race condition in multithreading? What is the Executor framework in Java? What is a virtual thread in Java 21? What is a thread pool? Describe the purpose of the java.util.concurrent package? How does the synchronized keyword achieve mutual exclusion internally? Why should you prefer ReentrantLock over synchronized in some cases? What is the difference between wait() and sleep()? What is the difference between notify() and notifyAll()? How does ConcurrentHashMap achieve thread safety? What is the difference between ConcurrentHashMap and Hashtable? Why should you use CompletableFuture instead of Future? How does the ForkJoinPool execute tasks? What is the difference between Runnable and Callable? How do you handle thread interruption in Java? What happens when a deadlock occurs in a multithreaded application? When should you use CountDownLatch instead of CyclicBarrier? How does compare-and-swap (CAS) work in atomic classes? What is the difference between ExecutorService shutdown() and shutdownNow()? How is the Java Memory Model relevant to concurrent programming? How does ThreadLocal work internally? What is the difference between CopyOnWriteArrayList and a synchronized ArrayList? When would you choose a fixed thread pool over a cached thread pool? How does a BlockingQueue support the producer-consumer pattern? What happens when you call start() twice on the same thread? Explain the lifecycle of a virtual thread in Java 21? Explain the internal working of structured concurrency in Java 21? Why does a synchronized block pin a virtual thread to its carrier thread? What is the difference between ScopedValue and ThreadLocal? Explain the internal working of the ForkJoinPool work-stealing algorithm? How can you optimize code to avoid false sharing? Why does LongAdder outperform AtomicLong under high contention? Why does the ABA problem occur in lock-free CAS-based algorithms? Explain the execution flow of a parallel stream operation? How do you troubleshoot a deadlock in a production Java application? What is the difference between StampedLock and ReadWriteLock? Which is better for high-throughput I/O - platform or virtual threads, and why? How can you optimize a thread pool for mixed CPU-bound and I/O-bound workloads? Explain the internal working of AbstractQueuedSynchronizer (AQS)? Why doesn't increasing the thread pool size always improve throughput?
Show more question and Answers...


Comments & Discussions