Sub topic 9
Pinning: when a virtual thread cannot unmount
This chapter assumes you know how virtual threads are mounted on carrier threads, that you have enabled spring.threads.virtual.enabled=true in a Spring Boot 3.2+ application, and that you can read a basic stack trace.
What pinning is
A virtual thread normally gives up its carrier thread when it blocks. The JVM copies the virtual thread's stack to the heap (unmounts it), and the carrier runs another virtual thread. Pinning is the case where the JVM cannot do this. The virtual thread stays mounted, and the carrier thread blocks along with it.
A few pinned threads cost little. Many pinned threads can use up all the carriers. The default scheduler has as many carriers as CPU cores, so on a 4-core machine, four virtual threads blocked while pinned can stall every other virtual thread in the process.
What causes pinning
On Java 21, a virtual thread is pinned in two situations:
- It blocks while inside a
synchronizedblock or method. - It blocks while a native method or foreign function is on its stack.
Short synchronized blocks that never block are harmless. The problem is blocking I/O, such as a JDBC call, an HTTP request, or a sleep, inside a monitor.
Fixing it with ReentrantLock
If a critical section blocks, guard it with a ReentrantLock instead of synchronized. A virtual thread waiting on a ReentrantLock can unmount.
import java.util.concurrent.locks.ReentrantLock;
class Counter {
private final ReentrantLock lock = new ReentrantLock();
private long value;
long increment() {
lock.lock();
try {
return ++value;
} finally {
lock.unlock();
}
}
}Always release the lock in finally. Unlike synchronized, the lock is not released automatically when an exception leaves the block.
Do not rewrite every synchronized block. Change the ones that surround blocking calls, and leave short in-memory sections alone.
Finding pinning
Start the application with the tracing property. When a virtual thread blocks while pinned, the JVM prints the stack trace.
java -Djdk.tracePinnedThreads=full -jar app.jar
jcmd <pid> Thread.dump_to_file -format=json threads.jsonRead the trace from the top. Frames marked as holding a monitor show the synchronized code responsible. Often the culprit is a library, such as an older JDBC driver or connection pool, rather than your own code. If so, upgrade the library or check its release notes for virtual thread support.
The jcmd command writes a JSON thread dump that includes virtual threads. A normal jstack dump does not list them.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Replacing every `synchronized` with a lock | Extra code, no benefit | Change only sections that block |
| Ignoring third-party libraries | Pinning you cannot see in your code | Read the trace, upgrade the library |
| Testing only at low load | Pinning stays hidden | Load test with many concurrent requests |
| Raising carrier count as a first fix | Hides the cause, uses more resources | Remove the pinning first |
Exercise
A service method is synchronized and calls a remote HTTP API inside it. On Java 21, under load, the whole application slows even though CPU use is low. Explain why, and give two steps to fix and confirm it.
Show answer
The virtual threads block on the HTTP call while holding a monitor, so they are pinned. Each pinned thread holds its carrier, and the number of carriers is about the CPU core count, so a few slow calls can occupy all carriers and stop other virtual threads from running. Fix it by guarding the section with a ReentrantLock (or by narrowing the synchronized block so the HTTP call happens outside it). Confirm by running with -Djdk.tracePinnedThreads=full and checking that the pinned stack trace no longer appears under load.