The Plain Picture#
A restaurant where cooks are expensive (each needs a whole workstation), so there are only 200 of them. Most of each cook's time is spent waiting — for the oven, for the delivery. Virtual threads are like giving each order its own lightweight "ticket": whenever the ticket is waiting on the oven, the cook puts it aside and works on another ticket. A handful of cooks can keep thousands of orders moving.
The Real Names#
A normal Java thread is a platform thread: a thin wrapper around an operating-system thread (Operating Systems Ch 7), with a ~1 MB stack. Thousands are fine; millions are not.
A virtual thread (Java 21) is a thread managed by the JVM, not the OS. The JVM runs many virtual threads on a small pool of platform threads called carrier threads (about one per core).
When a virtual thread does blocking I/O or sleeps, the JVM unmounts it from its carrier — saving its small stack to the heap — and runs another virtual thread on that carrier. When the I/O completes, it's mounted again (possibly on a different carrier).
Result: you write simple blocking, thread-per-request code (easy to read and debug), and get the scalability of an event loop (Operating Systems Ch 11) without callback-style code.
Under the Hood — measured#
Ten thousand tasks that each "wait" 100 ms (like a database or AI call):
try (ExecutorService platform = Executors.newFixedThreadPool(200)) {
for (int i = 0; i < 10_000; i++) platform.submit(() -> sleep(100));
}
try (ExecutorService virt = Executors.newVirtualThreadPerTaskExecutor()) {
for (int i = 0; i < 10_000; i++) virt.submit(() -> sleep(100));
}10,000 tasks x 100ms sleep: 200 platform threads 5223 ms | virtual threads 187 msWith 200 platform threads, the tasks run in 50 waves of 200: 50 × 100 ms ≈ 5 s. With one virtual thread per task, all 10,000 wait at the same time on 8 carrier threads: about 100 ms plus overhead. 28× faster, with simpler code.
Rules for using virtual threads#
Great for I/O-bound work — web requests, database queries, HTTP calls. No help for CPU-bound work — you still only have 8 cores; use a platform pool sized to the cores (Ch 2).
Don't pool them. They're cheap to create — make one per task (
newVirtualThreadPerTaskExecutor(),Thread.ofVirtual().start(...), orThread.startVirtualThread(...)).Limit access to scarce resources with a
Semaphore(Ch 9), not a small pool: 100,000 virtual threads can happily overwhelm a database that allows 100 connections (Networking 2: TCP, UDP and Ports Ch 12).Pinning: in Java 21, a virtual thread that blocks while inside a
synchronizedblock stays stuck to its carrier ("pinned"), losing the benefit; preferReentrantLockaround blocking calls in Java 21. (Java 24 removed this limitation forsynchronized.)Be careful with
ThreadLocal(Ch 12) — with millions of threads, per-thread caches multiply memory. Java's newer scoped values are the intended replacement.
Frameworks adopted them quickly: Spring Boot can run each request on a virtual thread with a single setting. The Python equivalent idea — cheap waiting
tasks — is asyncio (Python Concurrency (coming soon)).
Check yourself
4 questionsWhat's the difference between a platform thread and a virtual thread?
Show answerHide answer
A platform thread wraps an OS thread (big stack, expensive); a virtual thread is a lightweight JVM-managed thread run on a few carrier threads.
What happens when a virtual thread blocks on I/O?
Show answerHide answer
The JVM unmounts it from its carrier thread, which runs other virtual threads; it's remounted when the I/O completes.
Why did 10,000 sleeping tasks finish in ~190 ms on virtual threads but ~5,200 ms on 200 platform threads?
Show answerHide answer
Every task got its own virtual thread and they all waited simultaneously; the platform pool could only have 200 waiting at once (50 waves).
Should you use virtual threads for image processing? Should you pool them?
Show answerHide answer
No — it's CPU-bound, so use a core-sized platform pool. No — virtual threads are cheap; create one per task.