Virtual threads
A virtual thread is a lightweight thread scheduled by the JVM onto a small pool of platform threads. You can create millions of them. Blocking a virtual thread does not consume an OS thread for the whole wait (in the usual I/O cases).
Detailed Explanation
Platform threads (new Thread, a fixed pool) map closely to OS threads. Each one costs about a megabyte of stack reservation and a kernel object. You cannot have a thread per request at large scale. Virtual threads (Java 21, Project Loom) invert that: the JVM stacks them on the heap and mounts them on carrier platform threads when they run. When a virtual thread blocks on most JDK I/O, the JVM unmounts it and frees the carrier.
Write ordinary blocking code: socket.read(), Thread.sleep, future.get(). The style is simpler than reactive chains. Start them with Thread.ofVirtual().start(runnable) or Executors.newVirtualThreadPerTaskExecutor(). Do not pool virtual threads the way you pool platform threads; the pool's job was to limit expensive threads, and virtual threads are cheap. Limit blocking resources (connections) with a semaphore instead.
Pinning is the catch. A virtual thread that blocks while holding a synchronized monitor, or while inside some native frames, stays mounted on the carrier. A few pinned threads can stall the carriers. Prefer ReentrantLock over synchronized on hot blocking paths, or keep synchronized sections CPU-only and short. JDK 24+ reduces pinning for synchronized; still write short critical sections.
You reach for virtual threads when the workload is many concurrent blocking tasks (HTTP calls, JDBC). You keep a fixed platform pool or fork-join for CPU-bound work. Mixing: submit CPU work to a compute pool; use virtual threads for the I/O orchestration.
The cost of treating virtual threads as free is unbounded fan-out against a database of 20 connections. The cost of a huge thread-local on each virtual thread is heap pressure; prefer scoped values (Java 21+) for request context. The cost of debugging is new stack traces that show virtual frames; learn to read them.
Thread.sleep in a virtual thread is cheap parking. synchronized plus blocking I/O is the combination to audit.
Code Examples
import java.util.concurrent.Executors;
public class Many {
public static void main(String[] args) throws Exception {
try (var exec = Executors.newVirtualThreadPerTaskExecutor()) {
var f1 = exec.submit(() -> "a");
var f2 = exec.submit(() -> "b");
System.out.println(f1.get() + f2.get()); // prints: ab
}
}
}public class Start {
public static void main(String[] args) throws InterruptedException {
Thread t = Thread.ofVirtual().name("v-1").start(() ->
System.out.println(Thread.currentThread().isVirtual()));
t.join();
// prints: true
}
}Common Gotchas
Do not pool virtual threads to "save resources"
limit the downstream connections instead.
`synchronized` plus blocking I/O can pin a carrier
keep monitors short or use `ReentrantLock`.
CPU-bound work still needs a bounded compute pool
virtual threads do not add cores.
Huge `ThreadLocal` maps on millions of virtual threads exhaust memory
Huge `ThreadLocal` maps on millions of virtual threads exhaust memory