Skip to main content
PreparationZone Logo
PrepZone

Virtual Threads

Millions of concurrent tasks on a handful of carrier threads, and the pinning trap to avoid.

Pass checkpoint below

Why this matters

  • The thread-per-request model is the simplest way to write server code, and virtual threads make it scale again — you no longer have to choose between readable code and throughput.
  • Asynchronous and reactive code exists largely to avoid blocking OS threads. Remove that constraint and much of the complexity is no longer justified.
  • The failure mode, pinning, is specific and avoidable, and knowing it is the difference between virtual threads helping and quietly not helping.

The problem with platform threads

Java
Loading compiler…

A platform thread reserves a stack — typically around a megabyte of address space — and its creation and context switching go through the kernel. A few thousand is the practical ceiling, which is why pools exist. And when a pooled thread blocks on I/O, it is simply unavailable: 200 threads each waiting 100 ms means at most 2,000 requests per second regardless of how little CPU the work needs.

Virtual threads

Java
Loading compiler…
Platform thread
1 : 1 with an OS thread~1 MB stack each
Blocking wastes the threadOS thread sits idle
Thousands maximum
Virtual thread
Many : few carrier threadsGrows on demand
Blocking unmounts itCarrier runs other work
Millions are practical

Virtual threads help throughput for input/output-bound work. They do not make processor-bound code any faster.

A platform thread maps to one operating system thread, so a few thousand is the practical ceiling. Virtual threads are managed by the JVM and unmount while blocked, so millions are fine.
Java
Loading compiler…

How it works

Virtual thread runsMounted on a carrier thread
UnmountsStack parked on the heap
Carrier runs another virtual thread
Remounts and continues

A synchronized block can pin a virtual thread to its carrier. Prefer ReentrantLock in code that runs on virtual threads.

Blocking is what triggers the unmount. The carrier thread is freed for other virtual threads, which is how a handful of carriers serve enormous numbers of tasks.

The mechanism in four steps

  • A virtual thread runs on a carrier thread, taken from a dedicated ForkJoinPool sized to the number of cores.
  • When it hits a blocking operation that the JDK has adapted — socket I/O, sleep, a lock, a BlockingQueue — the JVM unmounts it, copying its stack to the heap and freeing the carrier.
  • The carrier immediately picks up another runnable virtual thread. No OS thread is idle.
  • When the operation completes, the virtual thread is remounted, possibly on a different carrier, and continues from exactly where it stopped.

This is why blocking is cheap: the cost of blocking a virtual thread is a stack copy on the heap, not a parked OS thread.

AspectPlatform threadVirtual thread
Backed byOne OS threadA heap-allocated stack, mounted on a carrier when running
StackFixed, around 1 MB reservedGrows and shrinks on the heap, starts tiny
Practical countThousandsMillions
Creation costA kernel call — pool themRoughly an object allocation — never pool them
Cost of blockingAn OS thread sits idleNear zero — the carrier is released
SchedulingPre-emptive, by the OSCooperative, by the JVM at blocking points
Good forCPU-bound workI/O-bound work
Priorities and thread groupsSupportedIgnored; always daemon, always normal priority
  • Backed by

    Platform threadOne OS thread
    Virtual threadA heap-allocated stack, mounted on a carrier when running
  • Stack

    Platform threadFixed, around 1 MB reserved
    Virtual threadGrows and shrinks on the heap, starts tiny
  • Practical count

    Platform threadThousands
    Virtual threadMillions
  • Creation cost

    Platform threadA kernel call — pool them
    Virtual threadRoughly an object allocation — never pool them
  • Cost of blocking

    Platform threadAn OS thread sits idle
    Virtual threadNear zero — the carrier is released
  • Scheduling

    Platform threadPre-emptive, by the OS
    Virtual threadCooperative, by the JVM at blocking points
  • Good for

    Platform threadCPU-bound work
    Virtual threadI/O-bound work
  • Priorities and thread groups

    Platform threadSupported
    Virtual threadIgnored; always daemon, always normal priority

Virtual threads do not make CPU-bound work faster — there are still only as many cores as there are cores.

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 6 sections

  • Stop pooling
  • Pinning — the one real trap
  • Virtual threads vs reactive vs CompletableFuture
  • Structured concurrency
  • What this means for application design
  • Common misreadings

Test yourself

Answer these before moving on — recall is what makes it stick.