skip to content

What happens when a virtual thread executes a blocking call such as Thread.sleep or blocking I/O?

level: middleimportance: must knowfreq 80%

answer

  1. blocking call -> unmount from carrier -> carrier freed
  2. Most JDK blocking ops unmount (sockets, sleep, locks)
  3. continuation parked on heap; remount on any carrier to resume
  4. Carrier freed while waiting -> sync code, async scale
  5. pinning = blocked but stays mounted (native frames)

basics

~20 s

In most JDK blocking calls the virtual thread unmounts from its carrier (the platform thread running it), freeing that carrier for other virtual threads, and is remounted to resume when the call can continue. Many file-system calls hold the carrier instead.

solid answer

~50 s

Most JDK blocking operations are virtual-thread-aware. When a virtual thread hits one — blocking socket I/O via java.net, NIO channels and the reworked java.io streams, Thread.sleep, java.util.concurrent locks, BlockingQueue, Future.get, etc. — the JVM captures (parks) its continuation and unmounts it from its carrier platform thread. The carrier is then free to mount and run other ready virtual threads, so a few carriers can serve millions of mostly-blocked virtual threads. Under the hood, blocking socket I/O registers interest with the OS (epoll/kqueue) and the carrier moves on; when data arrives the virtual thread becomes runnable and is remounted onto some carrier to continue exactly where it left off. Many file-system calls are the exception: they hold the carrier. The crucial point: for network I/O the OS thread is not the thing left waiting, so you write straightforward synchronous blocking code and still get the scalability of asynchronous I/O.

go deeper

for a junior

Knows that a blocking call pauses the virtual thread and frees the underlying platform thread to do other work.

for a middle

Explains unmount/remount onto carriers, lists which JDK blocking calls are VT-aware, and knows it's for I/O-bound (not CPU-bound) work.

for a senior

Describes the continuation/parking mechanism and OS-event integration, and explains pinning by JDK version: synchronized on 21 to 23 with the old ReentrantLock advice, native frames still at JDK 27.

for a principal

Reasons about carrier-pool sizing, pinning impact on tail latency under load, observability of mount/unmount, and when async/reactive still beats virtual threads.

## The problem virtual threads solve Blocking code is easy to read: `var data = socket.read(); process(data);`. But on a **platform thread** (a thread backed 1:1 by an OS thread), a blocking call means the underlying OS thread sits idle in the kernel, doing nothing, until the I/O completes. OS threads are scarce (thousands, not millions), so a server that holds one per in-flight request can't scale to many concurrent slow requests. The historical workaround was *asynchronous, non-blocking* code (callbacks, `CompletableFuture`, reactive streams) — scalable but hard to read and debug. Virtual threads give you the easy blocking style *and* the scalability. ## Mounting, unmounting, carriers, continuations - **Carrier thread**: a real platform (OS) thread from a small JVM-managed pool (a `ForkJoinPool` by default). It is the *engine* that actually executes code. - **Mount**: a virtual thread runs by being mounted onto a carrier — its stack frames are placed on the carrier and the carrier executes them. - **Continuation**: the saved state (stack) of a virtual thread that lets it be paused and later resumed exactly where it stopped. - **Unmount / park**: when the virtual thread can't make progress (it blocks), the JVM saves its continuation onto the heap and detaches it from the carrier. The carrier is now free to mount a *different* runnable virtual thread. ## What happens on a blocking call, step by step 1. The virtual thread calls a **JDK blocking operation** — these were specifically reworked to cooperate: blocking socket I/O (`java.net`, `java.nio` channels, `java.io` streams over sockets), `Thread.sleep`, `java.util.concurrent` locks (`ReentrantLock`, etc.), `BlockingQueue.take/put`, `Future.get`, `CompletableFuture.join`, latches, semaphores, and since JDK 24 `Object.wait` (on JDK 21 to 23 it held the carrier while the scheduler added a temporary one). 2. The JDK does not block the carrier. For socket I/O it registers interest with the OS event mechanism (epoll on Linux, kqueue on macOS, wepoll on Windows) and **unmounts** the virtual thread. 3. The carrier is returned to the scheduler and immediately picks up another ready virtual thread. **No OS thread is left idle.** 4. When the I/O completes (or the sleep elapses, or the lock is acquired), the virtual thread is marked runnable. The scheduler **remounts** it onto some available carrier — not necessarily the same one — and it resumes right after the blocking call as if nothing happened. Net effect: a handful of carriers (typically equal to CPU core count) can keep millions of virtual threads in flight, because at any instant most are unmounted, waiting. ## When it does NOT unmount cleanly **File-system operations.** Many cannot unmount because of limits in the OS: they hold the carrier while they run, and some compensate by temporarily adding a platform thread to the scheduler. **Pinning.** A virtual thread can get **pinned** — it blocks but stays mounted, tying up its carrier (which defeats the purpose). While pinned, that carrier can't serve other virtual threads. What pins depends on the JDK: - **JDK 21 to 23:** blocking **inside a `synchronized` block/method** (the JVM recorded the carrier as the monitor's owner) and blocking in **native code (JNI)** or a foreign function. The advice was `java.util.concurrent.locks.ReentrantLock` around frequent, long blocking calls. - **JDK 24 and later, including 25 LTS and 27:** JEP 491 lets virtual threads hold monitors independently of their carriers, so `synchronized` no longer pins and the `ReentrantLock` migration is no longer needed. Blocking while a native frame is on the stack, such as native code calling back into Java that blocks, still pins. ## Why this matters - You write **simple synchronous blocking code** and get **async-grade scalability**. - Virtual threads help **I/O-bound** workloads (lots of waiting). For **CPU-bound** work they give no scalability win — you're limited by cores, and unmounting never happens because the thread never blocks. - A `Thread.sleep` on a virtual thread is cheap (it just unmounts); the same sleep on a platform thread wastes a scarce OS thread.

  • What is pinning and how do you avoid it?
    Pinning is when a virtual thread blocks but stays mounted on its carrier, so the carrier can't run other virtual threads. On JDK 21 to 23 the classic causes were blocking inside synchronized code and in native (JNI) code, avoided by using ReentrantLock around blocking sections. Since JDK 24 synchronized no longer pins; avoid blocking under native frames, and find cases with the JFR jdk.VirtualThreadPinned event.
  • Do virtual threads make CPU-bound work faster?
    No. They scale concurrency for blocking/waiting (I/O-bound) work. CPU-bound work is bounded by core count, and a busy thread never blocks, so it never unmounts to free a carrier.

  • Saying the carrier thread blocks/waits during virtual-thread I/O
  • Claiming virtual threads speed up CPU-bound work
  • Forgetting that native frames (or synchronized on JDK 21-23) can pin the carrier
  • Thinking the thread always remounts onto the same carrier

virtual threads
Java 21pre-release 19
virtual threads unmount in synchronized and Object.wait
Java 24

Assumes JDK 27. Virtual threads were a preview in JDK 19 and 20 and are final since JDK 21. Since JDK 24 (JEP 491) blocking in synchronized code and in Object.wait unmounts a virtual thread, so JDK 25 LTS behaves like 27; on JDK 21 to 23 synchronized pins and Object.wait holds the carrier while the scheduler compensates.

context