Card 05/ 08

ComparisonDifficulty: Advanced1 min

Platform Threads vs Virtual Threads

One thread per connection used to be impossible at ten thousand connections; with virtual threads it is ordinary.

A platform thread is a Java thread backed by its own operating-system thread. A virtual thread, finalised in Java 21, is scheduled by the Java virtual machine itself, on a small pool of platform threads called carriers. The table compares their cost and scheduling, and the rule after it says when to use each.

A server holding ten thousand open connections could not use one platform thread per connection for twenty years, because each thread reserved around a megabyte of stack and needed a slot in the operating system's scheduler.

The two kinds of thread, on cost, scheduling and how many you can have
Compared onPlatform threadVirtual thread
Backed byOne operating-system threadA heap object, run on a carrier thread (a platform thread that virtual threads take turns on)
StackReserved up front, around a megabyteGrows and shrinks on the heap
Scheduled byThe operating systemThe virtual machine
Practical numberThousandsMillions
While blocked on input or outputIts operating-system thread is idleIt releases its carrier for someone else

Starting one virtual thread, and one per task through an executor:

java
Thread.ofVirtual().start(() -> handle(connection));

try (var pool = Executors.newVirtualThreadPerTaskExecutor()) {
    connections.forEach(c -> pool.submit(() -> handle(c)));
}

The decision rule. Use virtual threads for work that spends its time waiting: network calls, database queries, file reads. Use platform threads for work that spends its time computing, where there is nothing to release the carrier for, and a pool sized to the number of processors is the right shape.