Lesson · Virtual Threads & Structured Concurrency
Blocking Is Cheap Again: unmounting, carriers and pinning
15 min readBlocking is Cheap
When a virtual thread blocks on an operation the runtime supports, it unmounts: the carrier is released and runs the next task, and the blocked thread resumes later, possibly on a different carrier. That is why blocking went back to being the intended style. The exception is pinning: a virtual thread that cannot unmount keeps the carrier, and the causes of that are version-dependent (monitor pinning ended in Java 24) and worth diagnosing rather than guessing.
Blocking used to be the expensive thing
Every concurrent design of the last fifteen years was an argument with the cost of blocking. A platform thread that waited held an OS thread while it waited, so pools were sized to the penny, reactive pipelines returned their threads at every wait, and blocking inside a request handler became a sin.
Virtual threads remove the premise rather than the symptom. A blocked virtual thread does not hold a carrier: the JVM parks the task, and the platform thread moves on. Blocking goes back to being an ordinary thing to write in ordinary sequential code.
What remains is a short list of exceptions, and they matter in production: some blocking operations cannot hand the thread back, and those keep the carrier for the duration. This lesson is about the handoff, and about the cases where it does not happen.
Blocking got cheap because waiting no longer holds an OS thread.
What happens at the blocking point
When a mounted virtual thread reaches a blocking operation the JDK supports, the runtime takes a different path than it does for a platform thread. Instead of parking the OS thread in the kernel, it parks the virtual thread: the frames are kept as heap data (the stack chunks from the platform-versus-virtual lesson), the carrier is released, and the scheduler immediately has a free platform thread for the next runnable task.
That release is what unmounting means in practice. The OS thread never enters the kernel wait, and from the carrier's point of view there is only ever a sequence of short assignments, one per mounted virtual thread.
When the operation completes, the runtime submits the virtual thread back to the scheduler. It is mounted again, possibly on a different carrier, and continues on the line after the blocking call as if nothing had happened. One task can mount and unmount many times over its life; the API hides all of it, and Thread.currentThread() inside the task keeps returning the same virtual thread throughout.
The blocking call returns on the same statement, on a thread that may share nothing with the one that entered it.
state of a blocked virtual thread
the operation and the JDK version decide the outcome, not the call site aloneMOUNTED · running on a carrier
the virtual thread occupies the platform thread
UNMOUNTED · parked in the heap
- the frames wait as stack chunks; the carrier is released to the scheduler
- the thread is remounted later, possibly on a different carrier
- the common case: sleep, locks, semaphores, blocking queues, socket I/O
PINNED · the carrier stays captured
- the platform thread cannot run anything else until the block ends
- the scheduler does not compensate by adding carriers
- correctness is unaffected; long or frequent pinning costs scalability
When the blocking operation completes, the scheduler submits the virtual thread back and mounts it again. Diagnose pinning with the jdk.VirtualThreadPinned JFR event, enabled by default with a duration threshold; jdk.tracePinnedThreads was the older tool and is gone in Java 24.
Which operations release the carrier
The handoff happens for the operations the JDK reworked for virtual threads. In Java 21 the list covers almost everything a server does: Thread.sleep, the java.util.concurrent machinery built on LockSupport (locks, semaphores, blocking queues, the joins behind CompletableFuture), and blocking I/O on sockets and channels.
It does not cover everything. A few blocking operations cannot hand the thread back because of limitations at the operating system or JDK level, and there the scheduler compensates by temporarily creating more carriers, so the virtual thread occupies a platform thread while the machine keeps making progress. That is a cost, not a dead end.
And one kind of code does not block at all. A CPU-bound loop has no unmount point: the scheduler does not implement time sharing, so a long calculation holds its carrier until it ends or blocks. That boundary belongs to the next lesson, which is about choosing where virtual threads fit.
| Operation | What happens to the carrier |
|---|---|
Thread.sleep | unmounts: the carrier runs other virtual threads for the whole nap |
BlockingQueue.take() on an empty queue | unmounts: the wait is a parked virtual thread, not a blocked OS thread |
ReentrantLock contention, Semaphore.acquire() | unmounts: they park through LockSupport, which knows about virtual threads |
| Socket and channel reads | unmounts: the networking APIs were reworked for virtual threads |
Object.wait() | held the carrier in Java 21, with a temporary carrier added to compensate; unmounts from Java 24 on (JEP 491) |
| Many filesystem operations | the carrier is held; the scheduler temporarily compensates by adding carriers |
| A CPU-bound loop | not a blocking operation at all: the virtual thread keeps its carrier |
If the operation can hand the thread back, it does; if it cannot, the scheduler tries to make it not matter.
Pinning: when the carrier stays captured
Pinning is the sharper case. The virtual thread cannot unmount, the scheduler does not compensate by adding carriers, and the platform thread is captured for the whole duration of the block. In Java 21 there were two causes, and they have different fates.
The first was monitors. A virtual thread inside a synchronized block or method pinned its carrier whenever it blocked there, including when it merely waited to acquire a contended monitor. The reason was bookkeeping: the JVM recorded which platform thread held the monitor, so it could not let the virtual thread unmount without losing track of ownership.
The second is native code. A virtual thread executing a native method or a foreign function cannot unmount, because the native frame and its assumptions about its stack belong to the carrier. This one stays in every version.
The monitor cause ended in Java 24 with JEP 491, which taught the JVM to track monitors per virtual thread. From Java 24 on, blocking inside synchronized unmounts like any other wait, and the JDK's own guidance is that you can choose between synchronized and java.util.concurrent.locks purely on the merits. Code that is pinned on Java 21 can be perfectly healthy on Java 24, so the honest answer when you meet it is: it depends on the version you run.
- Confirm the version first: monitor pinning is a Java 21 to 23 problem; JEP 491 removed it in Java 24.
- On Java 21, keep long blocks out of monitors: move the I/O out of the
synchronizedsection, or use ajava.util.concurrentlock, which parks throughLockSupport. - Native frames pin everywhere: keep JNI and foreign function calls short on the virtual-thread path, or move them to a dedicated platform thread.
- Pinning hurts scalability, not correctness: the danger is sustained pinning across all carriers, where runnable virtual threads have nowhere left to run and the application can starve or deadlock while CPU sits idle.
A blocked virtual thread steps off the carrier; a pinned one rides it. Only the version and the native frame decide which you get.
Three fates, one carrier
The scene that makes this concrete is one carrier thread and two virtual threads. Watch them swap the carrier as each one blocks, then watch what changes when the block happens somewhere an unmount is not allowed.
Play it twice in your head: once by Java 21 rules, once by Java 24 rules. The only difference between the runs is who the JVM thinks owns the monitor.
One carrier served both tasks while they waited. That sentence is the whole feature.
blocking is cheap · one carrier, two virtual threads
carrier-1 · the only platform thread here
free
T1
ready
T2
ready
scheduler run queue: T1, T2: both ready
One carrier thread, carrier-1, and two virtual threads, T1 and T2. Both are ready; the scheduler mounts one at a time, and the other waits in its run queue.
Finding pinning
The JDK ships the diagnostic rather than leaving you to guess. JDK Flight Recorder emits jdk.VirtualThreadPinned when a virtual thread parks while pinned; the event is enabled by default and has a duration threshold, so a recording reports pinning that lasts long enough to matter instead of every instant of a park.
Before JFR, the way to find these paths was the system property jdk.tracePinnedThreads, which printed a stack trace with the monitor frames highlighted. JEP 491 removed it in Java 24: setting it has no effect there, because the case it existed to find is gone and the remaining cases are covered by the JFR event, which was extended to carry the reason and the carrier's identity.
Two practical notes. Measure on the JDK version you deploy, because the pinning picture genuinely differs between Java 21 and Java 24 and between library versions. And treat a pinning report as a scalability finding rather than a defect list: rewrite the paths that hold carriers for long durations, and leave the rest alone.
- Record with JFR in the environment that shows the symptom, and filter the recording for jdk.VirtualThreadPinned.
- Read the stack in the event: the offending frame is the blocking call, and the surrounding frames show what is holding the carrier, whether a monitor or a native frame.
- Fix on the merits: move the block out of the monitor, switch to a java.util.concurrent lock on Java 21, or shorten the native call.
- Re-record and compare. Pinning that stays rare and short is noise; pinning that scales with load is the thing to remove.
Pinning is a scalability signal: chase the carriers held for long durations, not every park.
What to remember
Four sentences cover the mechanism and its limits:
- Blocking unmounts: the virtual thread parks in the heap, the carrier is released, and the task resumes later, maybe elsewhere.
- Almost everything unmounts: sleep, locks, queues, socket I/O; a few OS-level operations are compensated with extra carriers instead.
- Pinning keeps the carrier: native frames pin everywhere,
synchronizedpinned until Java 24 (JEP 491). - CPU loops never block: no unmount point, no time sharing, so the carrier is held until the work ends.
Write blocking code again, then verify the two exceptions: the JDK version in front of you, and the native calls underneath you.
The synchronized method that swallowed a carrier
A service migrates to virtual threads and loses throughput under load. The profile shows idle CPU and carriers that are not doing the work, and the recording explains it: the hot path blocks on I/O inside a synchronized method, so on Java 21 every one of those calls pins a carrier for the whole wait. The same code on Java 24, where JEP 491 removed monitor pinning, scales with no rewrite at all. The trap is not synchronized; it is diagnosing pinning without checking the version first.
Interview angles
What does it mean for a virtual thread to unmount?+
The virtual thread parks itself as heap data and releases its carrier. The carrier is free to run another virtual thread, and when the blocking operation completes the runtime submits the parked thread back to the scheduler to be mounted again.
Mount and unmount can happen many times in one task, and the new carrier may not be the old one. From inside the task nothing is visible: Thread.currentThread() still returns the same virtual thread.
What is pinning, and what causes it?+
Pinning is a virtual thread that cannot unmount, so its carrier stays captured for the duration of the block. In Java 21 the causes were monitors (blocking inside synchronized) and native frames; JEP 491 removed the monitor cause in Java 24, and native frames pin in every version.
Common misconception: That pinning means the program is broken. The JEP is explicit: pinning does not make an application incorrect, it can hinder its scalability.
Follow-up: How do you find a pinned thread in production?
Which blocking operations unmount, and which do not?+
Thread.sleep, the LockSupport-based waits (locks, semaphores, blocking queues) and socket or channel I/O unmount. A few operations are limited by the OS or the JDK, such as many filesystem calls, and there the scheduler compensates by adding carriers. A CPU-bound loop never blocks, so it simply holds its carrier.
In Java 21 Object.wait() also failed to unmount; JEP 491 changed that in Java 24, so it belongs to the same version-sensitive family as monitor pinning.
Why did synchronized pin a virtual thread in the first place?+
The JVM recorded monitor ownership against the platform thread that held it, not against the virtual thread. If a pinned-thread-free virtual thread were allowed to unmount while holding a monitor, another virtual thread mounted on that carrier would look like the owner, and mutual exclusion would be lost.
JEP 491 fixed the bookkeeping: monitors are now acquired, held and released by the virtual thread itself, and mounting and unmounting maintain the mapping.
Does pinning break correctness?+
No. It restricts scalability, because a carrier held by a pinned thread cannot run anything else. Sustained pinning across all carriers can starve the scheduler or deadlock it, with the CPU idle while every platform thread is stuck.
Common misconception: That pinned threads throw or corrupt state. They do not; they simply behave like a platform thread for the duration.
How does the JVM help you find pinning?+
JDK Flight Recorder emits jdk.VirtualThreadPinned when a virtual thread parks while pinned; it is enabled by default with a duration threshold, and Java 24 extended it to report the reason and the carrier.
The older jdk.tracePinnedThreads=full property printed stacks with the offending frames highlighted, and JEP 491 removed it in Java 24. Either way, the fix is to stop holding the carrier for long: move the block out of the monitor, or shorten the native call.