Java / Java 21 Virtual Threads Interview questions
What happens when a virtual thread performs blocking I/O?
Most blocking APIs in the JDK - socket reads, java.net.http calls, file operations - have been reworked internally so that, when invoked from a virtual thread, they don't actually block the underlying carrier thread.
Instead, the JVM registers interest in the I/O event (often via an NIO selector under the hood), unmounts the virtual thread, and frees the carrier to run other work. Once the I/O completes, the virtual thread is marked runnable again and rescheduled onto an available carrier.
From the developer's point of view the code still looks and behaves like a normal blocking call - the asynchronous handling is entirely transparent.
More Related questions...