Java / JVM Architecture (Java21) Interview questions
Explain the execution flow of a virtual thread when it performs a blocking I/O call?
sequenceDiagram participant VT as Virtual Thread participant Sched as JVM Scheduler participant Carrier as Carrier Thread participant IO as I/O Subsystem VT->>Carrier: Mounted, running normally VT->>IO: Issue blocking read() Carrier->>Sched: Unmount VT continuation, park it Sched->>Carrier: Assign a different ready Virtual Thread IO-->>Sched: I/O completes, VT marked ready Sched->>Carrier: Mount VT (same or different carrier) when free Carrier->>VT: Resume execution after read()
The virtual thread starts mounted on a carrier thread and issues what looks like an ordinary blocking read; the JDK's I/O layer recognizes this call and, instead of parking the OS thread, unmounts the virtual thread's continuation and hands the carrier to the scheduler.
The scheduler immediately assigns that now-free carrier to another ready virtual thread, so no CPU-bound OS thread sits idle waiting on the disk or network.
Once the underlying I/O completes, the virtual thread is marked ready and re-mounted onto whichever carrier thread becomes available next, resuming execution right after the original blocking call as if nothing unusual happened.
More Related questions...