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
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
Virtual threads help throughput for input/output-bound work. They do not make processor-bound code any faster.
How it works
A synchronized block can pin a virtual thread to its carrier. Prefer ReentrantLock in code that runs on virtual threads.
The mechanism in four steps
- A virtual thread runs on a carrier thread, taken from a dedicated
ForkJoinPoolsized to the number of cores. - When it hits a blocking operation that the JDK has adapted — socket I/O,
sleep, a lock, aBlockingQueue— 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.
| Aspect | Platform thread | Virtual thread |
|---|---|---|
| Backed by | One OS thread | A heap-allocated stack, mounted on a carrier when running |
| Stack | Fixed, around 1 MB reserved | Grows and shrinks on the heap, starts tiny |
| Practical count | Thousands | Millions |
| Creation cost | A kernel call — pool them | Roughly an object allocation — never pool them |
| Cost of blocking | An OS thread sits idle | Near zero — the carrier is released |
| Scheduling | Pre-emptive, by the OS | Cooperative, by the JVM at blocking points |
| Good for | CPU-bound work | I/O-bound work |
| Priorities and thread groups | Supported | Ignored; always daemon, always normal priority |
Backed by
Platform threadOne OS threadVirtual threadA heap-allocated stack, mounted on a carrier when runningStack
Platform threadFixed, around 1 MB reservedVirtual threadGrows and shrinks on the heap, starts tinyPractical count
Platform threadThousandsVirtual threadMillionsCreation cost
Platform threadA kernel call — pool themVirtual threadRoughly an object allocation — never pool themCost of blocking
Platform threadAn OS thread sits idleVirtual threadNear zero — the carrier is releasedScheduling
Platform threadPre-emptive, by the OSVirtual threadCooperative, by the JVM at blocking pointsGood for
Platform threadCPU-bound workVirtual threadI/O-bound workPriorities and thread groups
Platform threadSupportedVirtual 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.