skip to content

When do virtual threads NOT help, and what are the main pitfalls when adopting them?

level: principalimportance: should knowfreq 52%

answer

  1. No win for CPU-bound work — cores are the limit
  2. Pinning (native; synchronized before 24) starves carriers
  3. Thread-locals scale badly per-task; prefer ScopedValue
  4. Cheap threads don't grow a small DB pool — bound with a Semaphore
  5. Don't pool VTs; segregate long CPU-bound work

basics

~20 s

Virtual threads help concurrency for blocking/I/O-bound work, not raw CPU speed. They don't help CPU-bound tasks, and you can lose the benefit through pinning (native calls; synchronized before JDK 24) or by overloading a small downstream resource.

solid answer

~50 s

Virtual threads raise concurrency for I/O-bound, mostly-blocked workloads; they don't speed up CPU-bound code, which is still limited by core count, and adding millions of CPU-hungry virtual threads just adds scheduling overhead. The main pitfalls: (1) Pinning — a virtual thread that blocks while pinned can't unmount, so it ties up a carrier; on JDK 21-23 blocking inside synchronized pins, while since JDK 24 mainly native frames do. (2) Thread-local misuse — per-task virtual threads make thread-locals and ThreadLocal-based caching/pooling either useless or memory-heavy at scale; prefer ScopedValue (final since JDK 25). (3) Forgetting downstream limits — cheap threads don't enlarge a 10-connection DB pool; bound the scarce resource with a semaphore. (4) Pooling virtual threads, which is an anti-pattern. (5) Carrier starvation if long CPU-bound work or pinning monopolizes the small carrier pool. Adoption means auditing libraries for pinning, not just swapping the executor.

go deeper

for a junior

Knows virtual threads help blocking/I/O work and don't speed up pure CPU work.

for a middle

Lists key pitfalls — pinning, downstream limits, don't pool — and that CPU-bound tasks gain nothing.

for a senior

Explains pinning causes/mitigations, thread-local issues with per-task threads, and bounding scarce downstream resources with a semaphore.

for a principal

Frames a full adoption strategy: auditing dependencies for pinning (native calls, and synchronized on JDK 21 to 23), segregating CPU work, observability (pinning events, carrier metrics), ScopedValue migration, and reasoning about where bottlenecks move.

## Recap of what virtual threads are for A **virtual thread** is a JVM-scheduled thread that, while running, **mounts** onto a **carrier** (a real OS thread) and **unmounts** (saving its stack to the heap) when it blocks on most JDK operations, freeing that carrier for other virtual threads. The benefit is being able to run a huge number of **mostly-blocked** tasks cheaply. That framing already tells you the limits. ## Where they do NOT help **CPU-bound work.** A task that keeps the CPU busy (number crunching, parsing, compression) is limited by the number of **CPU cores**, not the number of threads. Running it on a virtual thread doesn't make it faster; spawning millions of CPU-bound virtual threads just means they queue for the few carriers and add scheduling overhead. For CPU-bound parallelism, a bounded platform-thread pool sized to the cores (or a ForkJoinPool) remains the right tool. Virtual threads shine specifically when threads spend most of their life **blocked on I/O**. **Latency of a single request.** Virtual threads improve **throughput/concurrency**, not the latency of one isolated operation. One database call takes just as long. **File-heavy work.** Many file-system operations do not unmount the virtual thread: because of limits in the OS they hold the carrier while they run, and only some of them compensate by temporarily adding a platform thread to the scheduler. Such work runs correctly on virtual threads but gains less from them than network I/O does. ## The main pitfalls ### 1. Pinning **Pinning** is when a blocked virtual thread **cannot unmount** and so keeps occupying its carrier OS thread — reintroducing the scarce-thread limit. If many virtual threads pin at once, the small carrier pool is exhausted and everything stalls (**carrier starvation**). What pins depends on the JDK: - **JDK 21 to 23:** blocking inside a `synchronized` block or method, and blocking in a native method or foreign function. The advice was to replace `synchronized` around frequent, long blocking calls with `java.util.concurrent.locks.ReentrantLock`. - **JDK 24 and later, including 25 LTS and 27:** JEP 491 lets virtual threads hold monitors independently of their carriers, so `synchronized` no longer pins and that migration is no longer needed. Blocking while a native frame is on the stack, such as native code calling back into Java that blocks, still pins. Find offenders with the JFR `jdk.VirtualThreadPinned` event, enabled in JFR's default settings with a 20 ms threshold. The `-Djdk.tracePinnedThreads` property of JDK 21 to 23 was removed in JDK 24, and setting it has no effect. ### 2. Thread-local pitfalls A **thread-local** stores a value per thread. With a small pool of long-lived platform threads, thread-locals are a cheap per-thread cache. With **one virtual thread per task** and millions of them, thread-locals (a) lose their caching value (each task has a fresh one) and (b) can **balloon memory** if large objects are stuffed in them. Some libraries use thread-locals for pooling/buffers, which interacts badly. Prefer **`ScopedValue`** (immutable, scoped sharing; final since JDK 25) where possible, and audit libraries that lean on thread-locals. ### 3. Downstream resource limits Making threads unlimited does **not** make every resource unlimited. If 100,000 virtual threads all hit a **10-connection database pool**, 99,990 just block waiting — and you may have moved the bottleneck somewhere fragile or hidden timeouts. Bound the **scarce resource** explicitly (a `Semaphore` sized to the pool, or the pool's own limit) rather than relying on a thread cap you no longer have. ### 4. Pooling virtual threads (anti-pattern) Pools exist to reuse expensive resources; virtual threads are cheap, so **don't pool them**. Use `newVirtualThreadPerTaskExecutor()` (one per task). Pooling brings back stale thread-local state and artificial concurrency caps. ### 5. Carrier starvation from long CPU work Because virtual threads only yield the carrier at blocking points, a virtual thread that runs a **long CPU-bound stretch without blocking** holds its carrier the whole time. Enough of those and the carriers are monopolized, starving other virtual threads. Keep long CPU-bound work on a dedicated bounded pool, off the virtual-thread carriers. ## Adoption strategy (principal lens) Switching an executor to virtual threads is necessary but not sufficient. A migration checklist: 1. Audit hot paths and dependencies for pinning: native calls that block, and on JDK 21 to 23 `synchronized` around blocking calls. 2. Review thread-local usage, especially thread-locals that cache costly objects per thread. 3. Make downstream limits explicit with semaphores. 4. Keep long CPU-bound work on a separate bounded pool. 5. Add observability: the `jdk.VirtualThreadPinned` JFR event and, on JDK 24 and later, `jdk.management.VirtualThreadSchedulerMXBean` and `jcmd <pid> Thread.vthread_scheduler`. The payoff is simpler **blocking** code at scale — but only if the surrounding ecosystem actually unmounts.

  • You replaced a platform-thread pool with virtual threads but throughput didn't improve. What would you investigate?
    Check for pinning with the JFR jdk.VirtualThreadPinned event (blocking under native frames, or inside synchronized on JDK 21 to 23), a downstream bottleneck (DB/connection pool, rate limit) that the threads now all pile onto, CPU-bound stretches or file-heavy I/O holding carriers, and whether the workload is actually I/O-bound at all.
  • Why prefer ScopedValue over ThreadLocal with virtual threads?
    ScopedValue offers immutable, bounded-scope sharing without per-thread mutable state, avoiding the memory blow-up and stale-state problems of ThreadLocal across millions of short-lived virtual threads, and it composes cleanly with structured concurrency.

  • 'Virtual threads make everything faster' — they help blocking concurrency, not CPU speed
  • Treating an executor swap as the whole migration (ignoring pinning/thread-locals)
  • Assuming cheap threads remove all bottlenecks (DB pools, rate limits remain)
  • Running long CPU-bound loops on virtual threads and starving the carriers

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 virtual threads in JDK 24 (JEP 491), which also removed jdk.tracePinnedThreads, so JDK 25 LTS behaves like 27; on JDK 21 to 23 synchronized pins. ScopedValue is final since JDK 25.

context