Sub topic 11
Pinning: When a Virtual Thread Holds Its Carrier
Earlier chapters showed that a virtual thread unmounts from its carrier thread when it blocks. A carrier thread is the platform thread that actually runs the virtual thread's code. Unmounting frees the carrier for other virtual threads. In a few cases, a virtual thread cannot unmount. This chapter covers what that means, when it happens, and how to avoid it.
What Pinning Is
Pinning is when a virtual thread stays stuck to its carrier while it blocks. The virtual thread is waiting, but the carrier cannot run anything else. It sits idle until the virtual thread is done waiting.
The scheduler has only a small number of carriers, usually one per CPU core. If many virtual threads pin at the same time, all the carriers can get stuck. Other virtual threads that are ready to run then have to wait. Your program gets slower, but it still gives correct results.
When Pinning Happens
A virtual thread is pinned in these cases:
| Situation | JDK 21 to 23 | JDK 24 and later |
|---|---|---|
| Blocking inside a `synchronized` block or method | Pins | Does not pin (JEP 491) |
| Blocking inside a native method or foreign function | Pins | Pins |
A synchronized block is Java's built-in lock: only one thread at a time can run the code inside it. A native method is code written in a language such as C and called from Java.
Pinning only causes trouble when the thread blocks while pinned. A short synchronized block that does no waiting is fine.
Seeing the Cost
The program below runs 500 tasks two ways. Each task holds a lock and sleeps for 100 milliseconds. The first version uses synchronized. The second uses ReentrantLock, a lock class from java.util.concurrent.locks that lets the virtual thread unmount while it waits.
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;
public class PinningDemo {
interface Task {
void run(Object monitor, ReentrantLock lock) throws InterruptedException;
}
static void withSynchronized(Object monitor, ReentrantLock lock) throws InterruptedException {
synchronized (monitor) {
Thread.sleep(100); // pins on JDK 21-23
}
}
static void withReentrantLock(Object monitor, ReentrantLock lock) throws InterruptedException {
lock.lock();
try {
Thread.sleep(100); // the virtual thread can unmount
} finally {
lock.unlock();
}
}
static long timeMillis(Task task) {
long start = System.nanoTime();
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
for (int i = 0; i < 500; i++) {
Object monitor = new Object();
ReentrantLock lock = new ReentrantLock();
executor.submit(() -> {
task.run(monitor, lock);
return null;
});
}
}
return (System.nanoTime() - start) / 1_000_000;
}
public static void main(String[] args) {
System.out.println("synchronized: " + timeMillis(PinningDemo::withSynchronized) + " ms");
System.out.println("ReentrantLock: " + timeMillis(PinningDemo::withReentrantLock) + " ms");
}
}Each task gets its own lock, so the tasks never wait on each other. On JDK 21 with 8 cores, the synchronized version takes about 500 ÷ 8 × 100 ms, which is roughly 6 seconds, because only 8 tasks can sleep at once. The ReentrantLock version finishes in a little over 100 ms. On JDK 24 or later, both versions are fast.
Reducing Pinning
- Upgrade to JDK 24 or later if you can. That removes the
synchronizedproblem. - On JDK 21 to 23, if a
synchronizedblock does slow blocking work such as I/O or sleeping, replace it with aReentrantLock. Keepsynchronizedwhere the code inside is short and never blocks. - Move slow work out of the locked section when possible, so you hold the lock for less time.
- Find pinning instead of guessing. JDK Flight Recorder records a
jdk.VirtualThreadPinnedevent when a virtual thread blocks while pinned. On JDK 21 to 23 you can also start Java with-Djdk.tracePinnedThreads=fullto print a stack trace each time this happens. The chapter on observing virtual threads covers these tools.
Exercise
On JDK 21, a virtual thread enters a synchronized method and reads from a network socket for 2 seconds. What happens to its carrier thread, and what is one fix?
Show answer
The virtual thread is pinned, so the carrier stays idle for the full 2 seconds and cannot run other virtual threads. One fix is to replace synchronized with a ReentrantLock so the virtual thread can unmount while it waits. Another is to upgrade to JDK 24 or later.
Exercise
A virtual thread calls a native method that waits for a hardware device. Does upgrading to JDK 24 stop it from pinning?
Show answer
No. JDK 24 removed pinning for synchronized only. Blocking inside a native method or foreign function still pins the carrier on every JDK version.