Virtual threads
Java 21's lightweight threads. How they let you write simple blocking code that scales to millions of tasks, and the rules for using them well.
Virtual threads (final in Java 21) are the biggest change to Java concurrency in decades. They let you write plain, easy-to-read blocking code, one thread per request, that scales like complex asynchronous code.
01The problem they solve
A normal platform thread is a thin wrapper around an operating system thread. Each one needs about a megabyte of stack memory and is expensive to create and switch, so a server can afford a few thousand at most.
But most backend work is waiting: for the database, for another service. With one platform thread per request, threads sit blocked doing nothing while the server runs out of them. The old workaround was asynchronous code (CompletableFuture chains, reactive frameworks), which is powerful but hard to read and debug.
Loading the rest of the lesson…