Virtual Threads vs Platform Threads
Imagine a checkout service. Each request calls a payment provider, which takes about 200 milliseconds to reply. The service runs on a thread pool of 200 threads, the usual default for a Java web server. While a thread waits on the payment call it does nothing, but it is still taken. So the service tops out at about 200 requests in flight, or roughly 1,000 requests per second, even though the CPU sits at 5%.
You have two classic ways out. You can add threads, but OS threads are expensive, and 20,000 of them is a lot to ask of one machine. Or you can rewrite everything in an asynchronous style with an event loop, as in Async and Non-Blocking I/O, which scales well but turns simple code into callbacks and futures.
Virtual threads are a third way: threads so cheap that you can have one per request, or a million in total, while still writing plain blocking code. Java added them as a standard feature in Java 21 (the work was known as Project Loom). Go has had the same idea from the start with goroutines. This post explains what makes an ordinary thread expensive, how the runtime makes a cheap one, and when that actually helps.
💡 Why this matters in an interview: "how would you handle 10x the concurrent requests?" is a common follow-up for API aggregators, crawlers and notification senders. Knowing when virtual threads remove the bottleneck, and when they don't, is a strong senior-level answer.
Platform Threads: What a Thread Costs
Until Java 21, every java.lang.Thread was a thin wrapper around one operating system thread. Java now calls
these platform threads. Python's threading.Thread and C++'s std::thread are the same: one language thread
equals one OS thread (a 1:1 model).
An OS thread is a heavyweight object:
- A fixed stack. The OS reserves stack space up front, often around a megabyte of address space per thread by default. It must be large enough for the deepest call chain the thread might ever run, even if it never does.
- Kernel scheduling. The OS scheduler decides which thread runs on which core. Switching threads means a trip into the kernel and saving and restoring CPU state, which costs on the order of microseconds and disturbs CPU caches.
- Creation cost. Creating a thread is a system call plus memory setup. Fast enough for hundreds per second, too slow to do for every tiny task.
So we never create a thread per request. We keep a thread pool of a fixed size and reuse its threads. That solves creation cost, but the pool size becomes a hard cap on how many requests can be in progress at once.
Why the pool size caps throughput
There is a simple formula behind the checkout example, called Little's law:
requests in flight = throughput × time per request
If each request holds a thread for 0.2 seconds and we have 200 threads, the most we can serve is 200 / 0.2 = 1,000 requests per second. To serve 10,000 requests per second we need 2,000 requests in flight, which means 2,000 threads, all of them mostly waiting. The CPU is not the limit. The number of threads is.
This is the situation virtual threads are made for: many tasks, each spending most of its time waiting.
Run the same numbers with cheap threads. If a waiting thread costs a few kilobytes instead of a reserved megabyte and a kernel thread, then 2,000 or even 20,000 requests in flight is a few tens of megabytes of heap. The ceiling moves from "how many threads can the machine afford" to the limits that actually matter: CPU, memory per request, and how much load the payment provider and the database will accept.