skip to content

For which kinds of workloads do virtual threads help, and where do they give no benefit?

level: middleimportance: must knowfreq 65%

answer

  1. I/O-bound = mostly waiting -> VTs shine
  2. CPU-bound = mostly computing -> bounded by cores, no VT benefit
  3. Blocking socket I/O unmounts the VT
  4. VTs = reactive scalability, imperative simplicity
  5. CPU work: size to availableProcessors(), use a platform/fork-join pool

basics

~20 s

Virtual threads shine for I/O-bound work: tasks that mostly wait on the network, a database or other services. For CPU-bound work they give no benefit, because the limit is the number of CPU cores, not the number of threads.

solid answer

~50 s

Virtual threads win when tasks are I/O-bound — they spend most of their time blocked waiting on the network or a database. When a virtual thread blocks on a socket, a lock or a queue, the JVM unmounts it from its carrier thread, so a handful of OS threads can drive millions of concurrent waiting tasks (many file-system calls are the exception: they hold the carrier while they block). That lets you write simple blocking, thread-per-request code that scales like async/reactive code without the callback complexity. For CPU-bound work — heavy computation that never blocks — virtual threads give no speedup: throughput is bounded by the number of physical cores, so size concurrency to roughly the core count. Spawning a million virtual threads for CPU work adds scheduling overhead with no parallelism gain, because they all still compete for the same cores.

go deeper

for a junior

Can state that virtual threads help I/O-bound (waiting) work and not CPU-bound (computing) work.

for a middle

Explains the unmount-on-block mechanism for I/O and that CPU throughput is capped by cores, so virtual threads add no parallelism there.

for a senior

Frames virtual threads as enabling thread-per-request simplicity at reactive scale for I/O, sizes CPU work to core count with a fork-join/platform pool, and is aware of pinning caveats.

for a principal

Reasons about mixed workloads and where to place CPU work vs I/O work in the same service, weighs migrating from reactive stacks, and accounts for pinning/native-call edge cases and JDK-version behavior when advising adoption.

## Two kinds of work Every task spends its time in one of two ways: - **I/O-bound**: most of the elapsed time is spent *waiting* — for a network response, a database query, a disk read. The CPU is idle during the wait. - **CPU-bound**: most of the time is spent *computing* — hashing, encoding, number-crunching. The CPU is busy the whole time. The right concurrency strategy depends on which you have. ## How virtual threads handle blocking A **virtual thread** runs on top of a **carrier** (a platform/OS thread). The JVM's magic is this: when a virtual thread executes a *blocking* operation (network read, `sleep`, most JDK blocking calls), the JVM **unmounts** it from its carrier and parks it as a cheap heap object. The carrier is now free to mount and run a *different* virtual thread. When the I/O completes, the parked virtual thread is **remounted** on some carrier and continues. Not every blocking call works this way: many file-system operations, such as an ordinary `FileInputStream` read, block the carrier itself until they return. So a few carriers (by default about as many as you have cores) can keep millions of *waiting* virtual threads alive. This is why virtual threads are transformative for I/O. ## Why I/O-bound work benefits Before virtual threads, handling 10,000 concurrent slow network requests with one platform thread each was risky: JEP 444's own example of 10,000 platform threads might crash, depending on the machine and operating system. The workaround was **asynchronous / reactive** programming (callbacks, `CompletableFuture`, reactive streams), which is fast but hard to read, debug, and profile (stack traces become meaningless). Virtual threads let you go back to the simple **thread-per-request**, straight-line blocking style — `var data = http.get(url); var row = db.query(...);` — and still scale to millions of concurrent requests, because the blocking calls unmount instead of holding an OS thread. You get reactive-level scalability with imperative-level simplicity. ## Why CPU-bound work does NOT benefit For pure computation there is no blocking, so nothing ever unmounts. The work can only proceed as fast as the **physical cores** can execute it. If you have 8 cores, at most 8 CPU-bound tasks make progress at once — whether you spawn 8 threads or 8 million. Extra threads beyond the core count don't add parallelism; they only add **scheduling and context-switch overhead** and memory churn. So for CPU-bound work the guideline is unchanged from the classic advice: size your concurrency to roughly the number of cores (e.g. `Runtime.getRuntime().availableProcessors()`), typically a **fixed platform-thread pool** or a **work-stealing / fork-join pool**. Virtual threads neither help nor (much) hurt here, but they add no value, so there is no reason to reach for them. ## A nuance: blocking that does not unmount Some blocking keeps a virtual thread on its carrier, so it costs a platform thread for as long as it waits: - **`synchronized` on JDK 21 to 23.** Blocking inside a `synchronized` block or method **pinned** the virtual thread to its carrier. JEP 444's mitigation was to change `synchronized` code that runs often and guards long I/O to a `ReentrantLock`. - **`synchronized` from JDK 24.** JEP 491 lets virtual threads acquire, hold and release monitors independently of their carriers, so on JDK 25 and 27 that migration is no longer needed. - **Native frames, still.** A native method or foreign function on the stack can still pin, and JEP 491 keeps that limit. - **File-system calls.** Many still hold the carrier for as long as they block, as above. ## Summary I/O-bound: virtual threads are ideal — simple blocking code that scales like async. CPU-bound: no benefit — parallelism is capped by cores, so use a small pool sized to the core count.

  • What is the maximum useful parallelism for a CPU-bound task on an 8-core machine?
    About 8 — equal to the number of physical cores. Beyond that, extra threads (virtual or platform) only add scheduling overhead and contention, since at most 8 instructions streams can truly run at once.
  • Why are virtual threads described as giving 'reactive scalability with imperative simplicity' for I/O?
    You can write simple, readable straight-line blocking code (one thread per request) instead of callbacks/reactive streams, yet still serve millions of concurrent I/O-bound requests, because each blocking call unmounts the virtual thread instead of holding an OS thread.

  • Claiming virtual threads make CPU-bound code faster — core count is the ceiling
  • Thinking a million virtual threads = a million cores of parallelism
  • Believing virtual threads replace async/reactive for raw throughput rather than for simplicity at scale on I/O
  • Assuming every blocking call unmounts: native frames, many file-system calls and synchronized on JDK 21-23 keep a VT on its carrier

virtual threads
Java 21pre-release 19

Assumes JDK 21 or later, where virtual threads are final after previews in JDK 19 and 20. The pinning nuance differs by release: blocking inside synchronized pinned on JDK 21 to 23 and no longer does from JDK 24, so JDK 25 and 27 behave the same.

context