Virtual threads and structured concurrency question 113 of 180 mid
What is a virtual thread, and how is it scheduled?
- A thread with a smaller stack
- Correct: A JVM-managed thread whose stack lives on the heap, multiplexed by a work-stealing scheduler onto a small pool of platform carrier threads
- A green thread implemented by the OS
- Another name for a fiber in a library
A virtual thread is an ordinary java.lang.Thread whose execution is a continuation the JVM can park
and resume. When it blocks, the JVM saves its stack to the heap, unmounts it from its carrier, and the
carrier runs someone else. When the blocking call finishes, the continuation is mounted on any
available carrier and resumed.
The scheduler is a dedicated ForkJoinPool in FIFO mode, sized to availableProcessors() by default.
Creating one costs roughly a few hundred bytes rather than a megabyte of reserved stack, so millions
are practical.
Everything else stays the same: Thread.currentThread(), thread dumps, try/catch, debuggers and
stack traces all work, which is the entire point.
Quiz yourself on Virtual threads and structured concurrency Suggest a correction