skip to content

What specific code constructs cause a virtual thread to pin, and which common blocking operations do NOT?

level: middleimportance: must knowfreq 60%

answer

  1. Two main causes: synchronized (pre-24) + native
  2. Blocking is fine if the VT can unmount
  3. Socket I/O, sleep, ReentrantLock, BlockingQueue → no pin
  4. Harm = blocking WHILE pinned
  5. JEP 491 removes synchronized pinning; JNI remains

basics

~10 s

Pinning is caused by blocking inside a synchronized block/method (JDK 21 to 23) and inside native/JNI calls. Ordinary blocking like socket I/O, Thread.sleep, BlockingQueue, and ReentrantLock do NOT pin—the virtual thread unmounts normally.

solid answer

~40 s

There are two main pinning causes. First, blocking while holding a monitor—i.e., inside a `synchronized` block or method—because (pre-JDK 24/JEP 491) the monitor is bound to the carrier OS thread. Second, blocking inside a native method frame (a JNI call into C code), because the JVM can't unmount across native frames. Crucially, most 'blocking' you write does NOT pin: Loom reimplemented the JDK's blocking operations to unmount. So socket I/O, `Thread.sleep`, `java.util.concurrent` locks like `ReentrantLock`, `BlockingQueue`, `CompletableFuture.get`, and `Future.get` all unmount cleanly. File-system calls are a separate case: they don't pin, but many hold the carrier while they run. The trap is therefore narrow: it's specifically `synchronized` that *also blocks inside* (on JDK 21–23), and native code. A non-blocking `synchronized` section (a quick increment) pins only briefly and is fine.

go deeper

for a junior

Can name synchronized (on JDK 21 to 23) and native/JNI as the two main pinning causes and knows that ordinary blocking (sleep, socket I/O) is safe.

for a middle

Explains why I/O and j.u.c locks were reimplemented to unmount, and recognizes that the trap is specifically blocking inside synchronized (on JDK 21 to 23) or native frames.

for a senior

Can audit a real codebase/library for hidden synchronized-around-blocking and native drivers, and reasons about which JDK version changes the answer for synchronized.

for a principal

Sets dependency-vetting policy (which drivers/libraries are virtual-thread-safe), tracks JDK-version-dependent behavior across the fleet, and weighs FFM/JNI native boundaries in architecture decisions.

## The mental model: blocking is fine, *un-unmountable* blocking is not Virtual threads are designed so that when they block, they **unmount** from their carrier OS thread and free it. Pinning is the narrow set of cases where unmounting is impossible. So the question 'does X cause pinning?' reduces to 'can the JVM unmount the virtual thread while X blocks?' ## The two main causes of pinning **1. Blocking inside a `synchronized` block or method (monitor pinning).** A `synchronized` region acquires an object's *monitor* (an intrinsic lock). The monitor's ownership is recorded against the underlying OS thread. To unmount the virtual thread you'd have to move that ownership to a different carrier later, which the pre-JDK-24 runtime can't do. So while a virtual thread sits inside `synchronized` and then blocks (e.g., waits, does I/O, calls a slow method), it stays pinned to its carrier. Note two subtleties: - The harm is *blocking while pinned*. A `synchronized` block that just mutates a field and returns pins only briefly—negligible. - **JDK 24 / JEP 491** reworked the runtime so virtual threads *can* unmount inside `synchronized`. On JDK 24+ this cause is essentially gone. The advice below applies primarily to **JDK 21–23**. **2. Blocking inside a native method frame (JNI).** The **Java Native Interface (JNI)** lets Java call functions written in C/C++. When execution is down in native code, the Java stack is interleaved with native frames the JVM can't relocate, so it cannot unmount. If that native call blocks (e.g., a native database driver doing blocking I/O), the virtual thread pins. This cause remains even on JDK 24+. (A `Foreign Function & Memory` / FFM downcall has similar constraints.) **3. Rare: class loading and class initialization.** JEP 491 lists a few remaining cases where a VM call sits on the stack: blocking while a class is being loaded, and blocking inside a static initializer. A virtual thread that sleeps inside `static { ... }` is reported by JFR with the reason `VM call to ...<clinit> on stack`. Since JDK 26, a virtual thread merely *waiting* for another thread to finish a class initializer usually unmounts instead of pinning. ## What does NOT pin (the reassuring part) Project Loom went through the JDK and reimplemented blocking points to be unmount-aware. These all unmount cleanly and do **not** pin: - **Network I/O**: `Socket`, `SocketChannel`, `java.net.http.HttpClient`. - **`Thread.sleep`, `LockSupport.park`**, and **`Object.wait` since JDK 24** (on JDK 21 to 23 it runs inside `synchronized` and pins). - **`java.util.concurrent` synchronizers**: `ReentrantLock`, `ReentrantReadWriteLock`, `Semaphore`, `CountDownLatch`, `CyclicBarrier`, `BlockingQueue` operations. - **`Future.get`, `CompletableFuture.get`, `ExecutorService.invokeAll`**. **A caveat that is not pinning:** many *file-system* operations (`java.io.FileInputStream.read`, most `FileChannel` reads) still do not unmount at JDK 27: OS limits make them hold the carrier for the length of the call, and only some compensate by temporarily adding a carrier. Separately, since the JDK 19 preview of virtual threads the JDK's own buffered streams (`BufferedInputStream`, `PrintStream` and friends) use explicit locks rather than monitors when used directly, so that they would not pin. ## Putting it together When auditing code for pinning you are essentially looking for: (a) on JDK 21 to 23, `synchronized` that wraps a blocking call, and (b) on any JDK, native/JNI libraries that block. Apart from those, file-system calls (which hold the carrier) and the rare class-loading cases, blocking unmounts. This is why the JDK 21 to 23 advice was so specific: 'replace blocking `synchronized` with `ReentrantLock`,' not 'avoid blocking.'

  • If I have a synchronized method that does no blocking—just updates a counter—is that a pinning problem?
    No. It pins only while it executes; there's no blocking while pinned, so the carrier is freed immediately. Pinning is a problem only when a thread blocks while pinned.
  • Why might a third-party JDBC driver still cause pinning on JDK 24?
    JEP 491 fixed synchronized pinning but not native pinning. If the driver does blocking I/O through a JNI native call, the virtual thread can't unmount across the native frame and pins. The driver may also use synchronized internally on older JDKs.

  • Listing socket I/O or Thread.sleep as pinning causes—they unmount cleanly
  • Saying ReentrantLock pins (it's the recommended non-pinning lock)
  • Forgetting JNI/native as a cause and naming only synchronized
  • Claiming a brief non-blocking synchronized increment is a pinning problem

virtual threads
Java 21pre-release 19
synchronized no longer pins virtual threads
Java 24

Assumes JDK 27. Virtual threads were a preview in JDK 19 and 20 and are final since JDK 21. Synchronized stopped pinning in JDK 24 (JEP 491), which JDK 25 LTS shares; on JDK 21 to 23 synchronized pins. JDK 26 made a virtual thread that waits for another thread's class initializer unmount in most cases, so on JDK 25 that wait still pins.

context