Quick revision
The short answer
A virtual thread is pinned when it blocks but cannot unmount, so it keeps holding its carrier thread for the whole wait. Before JDK 24, blocking inside a synchronized block or method, or in Object.wait(), pinned the virtual thread, because monitors were tied to the OS thread. JEP 491 in JDK 24 lets virtual threads acquire, hold and release monitors independently of their carriers, which removes most synchronized pinning; pinning still happens in a few cases, notably when native methods or foreign functions are on the stack. Short pinned sections are harmless, but long blocking while pinned reduces throughput.
What to remember
In this answer, we saw that pinning means a virtual thread blocks while still mounted, holding its carrier. Before JDK 24, blocking inside synchronized or in Object.wait() pinned, because monitors were owned by the OS thread, and we reproduced how that turns 1,000 short sleeps into many rounds limited by the carrier count. JEP 491 in JDK 24 lets virtual threads own monitors independently of carriers, removing most synchronized pinning, while native and foreign function frames on the stack still pin. Short pinned sections are harmless; long blocking while pinned is what reduces throughput.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A virtual thread is pinned when it blocks but cannot unmount, so it keeps holding its carrier thread for the whole wait. Before JDK 24, blocking inside a synchronized block or method, or in Object.wait(), pinned the virtual thread, because monitors were tied to the OS thread. JEP 491 in JDK 24 lets virtual threads acquire, hold and release monitors independently of their carriers, which removes most synchronized pinning; pinning still happens in a few cases, notably when native methods or foreign functions are on the stack. Short pinned sections are harmless, but long blocking while pinned reduces throughput.
Why this question is asked
Pinning is the main reason a service can get slower after switching to virtual threads. With only as many carriers as CPU cores, a few threads that hold their carriers while waiting can stop every other virtual thread from running. It is also a topic where the right answer changed recently, with JDK 24, so interviewers use it to check whether our knowledge is current. In this answer, we will recap mounting and carriers, define pinning, see why synchronized pinned before JDK 24 and reproduce the effect, look at what JEP 491 changed and what remains, and finish with when pinning actually matters.
1A Quick Recap of Mounting
A virtual thread runs on a carrier, one of a small number of platform threads in the scheduler's pool, about one per CPU core by default. While it runs, it is mounted on that carrier. When it blocks on an operation the JDK supports, such as a socket read or a sleep, it unmounts: its stack frames are saved to the heap and the carrier is freed to run another virtual thread. That is how a few carriers can serve thousands of waiting virtual threads.
2What Pinning Is
Pinning is the case where a virtual thread blocks but cannot unmount. It stays attached to its carrier for the whole time it waits, so that carrier can do nothing else. From the program's point of view everything still works; the blocking call still returns. But one of the few carriers is out of service until it does.
If only a few threads are pinned briefly, other carriers pick up the slack and nobody notices. If, under load, as many threads are pinned as there are carriers, every other virtual thread waits in the scheduler's queue, even though the CPU is idle. That is the failure mode: throughput collapses for no visible reason.
3Why synchronized Pinned Before JDK 24
Java's synchronized keyword uses a monitor, a lock built into every object. Before JDK 24, the JVM recorded the owner of a monitor as the OS thread, effectively the carrier, not the virtual thread. If a virtual thread unmounted while holding a monitor, a different virtual thread on the same carrier would appear to own it, which would break mutual exclusion. So the JVM simply did not unmount a virtual thread that held a monitor. Any blocking call inside synchronized, such as a socket read, a sleep, or Object.wait(), pinned the virtual thread. We can see the effect directly.
| Situation | JDK 21 to 23 | JDK 24 and later |
|---|---|---|
| Socket read, no lock held | unmounts | unmounts |
| Socket read inside synchronized | pins | unmounts |
Object.wait() inside synchronized | pins (pool compensates) | unmounts |
| Waiting to enter a contended synchronized | pins | unmounts |
| Blocking with a native frame on the stack | pins | pins |
ReentrantLock.lock() and await() | unmounts | unmounts |
Notice that there is no lock contention here: every task has its own lock. The slowness comes entirely from pinning. On JDK 21, each sleeping task holds its carrier, so only 8 tasks can sleep at once and the 1,000 tasks take 125 rounds of 100 milliseconds. On JDK 24 and later the same code unmounts during the sleep and finishes in about one round. Replacing synchronized with a ReentrantLock gives the fast result even on JDK 21, because java.util.concurrent locks always supported unmounting.
4What JEP 491 Changed
JEP 491, Synchronize Virtual Threads without Pinning, delivered in JDK 24, changed how the JVM implements monitors so that virtual threads can acquire, hold and release them independently of their carriers. The monitor's owner is now the virtual thread itself. That means a virtual thread holding a monitor can unmount when it blocks and be mounted later on any carrier, still owning the monitor. A virtual thread waiting to enter a contended synchronized block also unmounts instead of holding its carrier, and Object.wait() unmounts as well.
The practical effect is that the most common source of pinning, libraries using synchronized around I/O, disappears without any change to the libraries. It also made the old diagnostic option jdk.tracePinnedThreads unnecessary, and it was removed in JDK 24; the JFR event jdk.VirtualThreadPinned remains the way to find pinning.
5Where Pinning Still Happens
Some cases remain. The main one is native code: if a virtual thread calls a native method through JNI, or a foreign function through the Foreign Function and Memory API, and blocking happens while that native frame is on its stack, for example because the native code calls back into Java which then blocks, the virtual thread cannot unmount, because the JVM cannot move native frames to the heap. There are also a few cases involving class initialization, such as blocking inside a static initializer. JEP 491 lists the remaining cases, and they are worth checking for libraries that use native code heavily.
Pinning is not always a problem. A synchronized block that updates a few fields and never blocks pins for microseconds, which costs nothing measurable. What hurts is blocking for a long time while pinned, such as network I/O inside a native call, under high concurrency. JFR records jdk.VirtualThreadPinned when a pinned thread blocks for longer than a threshold, 20 milliseconds by default, with a stack trace that shows where.
- Native method or foreign function on the stack?JNI or FFM call in progressYesPinsNoContinue
- JDK 21 to 23 and inside synchronized?holding or entering a monitorYesPinsNoContinue
- Adapted blocking operation?socket, sleep, lock, queueYesUnmounts, carrier freedNoBlocks; some file I/O compensates
3 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Is pinning a correctness problem?
No. A pinned virtual thread still behaves correctly; the blocking call still returns the right result. Pinning is a capacity problem: carriers are held while threads wait, so other virtual threads cannot run. In the extreme it can look like a deadlock, when every carrier is pinned waiting for something that needs a carrier to happen.
Should we still replace synchronized with ReentrantLock on JDK 25?
Not to avoid pinning, since JEP 491 handles it. ReentrantLock is still worth using when we need its features, such as tryLock with a timeout, interruptible locking or several conditions. On JDK 21, replacing synchronized around blocking calls in hot paths is the main way to avoid pinning.
How do we detect pinning?
Record JFR with the jdk.VirtualThreadPinned event enabled, which it is by default with a 20 millisecond threshold, and look at the stack traces under load. On JDK 21 to 23 the system property jdk.tracePinnedThreads also printed stacks, but it was removed in JDK 24.
7Conclusion
In this answer, we saw that pinning means a virtual thread blocks while still mounted, holding its carrier. Before JDK 24, blocking inside synchronized or in Object.wait() pinned, because monitors were owned by the OS thread, and we reproduced how that turns 1,000 short sleeps into many rounds limited by the carrier count. JEP 491 in JDK 24 lets virtual threads own monitors independently of carriers, removing most synchronized pinning, while native and foreign function frames on the stack still pin. Short pinned sections are harmless; long blocking while pinned is what reduces throughput.
Open with
Pinning is when a virtual thread blocks but can't unmount, so it keeps its carrier for the whole wait. Since there are only about as many carriers as cores, if enough threads are pinned at once, everything else waits in the queue even though the CPU is idle.
Then, if they want more
Before JDK 24 the big cause was synchronized. Monitors were owned by the OS thread, really the carrier, so the JVM couldn't unmount a virtual thread holding one. Any blocking inside synchronized, a socket read, a sleep, Object.wait, pinned it.
A quick demo: a thousand virtual threads each doing a 100 millisecond sleep inside synchronized, each on its own lock, take about twelve seconds on JDK 21 with 8 carriers, and about a tenth of a second on 24.
JEP 491 in JDK 24 changed monitors so the virtual thread itself owns them, which means it can unmount while holding a monitor, or while waiting to enter one, and Object.wait unmounts too. So most synchronized pinning is gone without changing libraries.
It still pins when a native method or foreign function call is on the stack, plus a few class initialization cases listed in the JEP. And short pinned sections are harmless; it's long blocking while pinned under load that hurts. To find it, I'd use the JFR jdk.VirtualThreadPinned event.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 24, Java 21
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- JEP 444, Virtual Threads
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is pinning for virtual threads, what caused it before JDK 24, and what changed with...": https://scrivio-interview-library.netlify.app/java/virtual-threads/cbd38534dd/