Java 21 virtual threads, a year later
Virtual threads changed how I write blocking Java services, but they did not repeal backpressure, connection pools, or physics. What held up, what did not, and the defaults I use now.
TL;DR: A year of using virtual threads in production settled one thing: they are a programming model feature, not a scalability feature. They make blocking code readable and cheap to run, but they do not create database connections, CPU, or backpressure, so pools and limits still matter.
Who this is for: Java developers already using virtual threads, or deciding whether to adopt them.
Java 21 made virtual threads a finished feature, and a year of using them in anger has settled into a fairly clear picture for me. They are a real improvement to how server-side Java gets written. They are also widely misunderstood in one specific way: people treat them as a scalability feature, when they are really a programming model feature.
1. Words to know#
If you are new to virtual threads, this box is the whole vocabulary of the article. Come back to it whenever a word looks unfamiliar.
- Virtual thread: a cheap thread managed by the JVM instead of the operating system, so you can have one per request.
- Platform thread: an ordinary operating system thread, expensive to create, so servers share a fixed pool of them.
- Carrier thread: a platform thread that does the actual running for virtual threads. A virtual thread parks when it blocks, and its carrier runs another one.
- Pinning: a virtual thread that blocks inside a synchronized block, or inside a native call, cannot be unmounted, so it holds its carrier thread hostage.
- Backpressure: the way a system slows down new work when it is overloaded, instead of accepting more than it can handle.
- Bulkhead: a limit that isolates one part of a system from another, so one overloaded service cannot take down the rest.
- Structured concurrency: a group of subtasks run inside a scope that waits for all of them and cancels the rest if one fails.
- I/O bound: work that mostly waits on a network or a database, rather than on the CPU.
2. What they actually give you#
The classic problem is simple. A thread-per-request service does mostly blocking work: read a request, call a database, call two services, write a response. Platform threads are too expensive to give one to every request, so we built async and reactive machinery to share them, and paid for it with stack traces that read like ransom notes and code nobody wants to debug at 3 a.m.
Virtual threads give the threads back. You write straight-line blocking code, the runtime parks the virtual thread when it blocks, and a small pool of carrier threads does the actual running. The code looks like 2009 and scales like it should.
Figure 1: Platform threads are one per request and sit idle when blocked. Virtual threads park when they block, so a few carrier threads can run thousands of them.
3. Where they shine#
Anywhere the work is I/O bound and the concurrency is naturally per-request: API gateways, CRUD services, fan-out calls to internal systems, report generation. In these services I have happily deleted reactive plumbing and replaced it with ordinary code that a new hire can read. That readability is not a soft benefit. It is most of the benefit.
They also pair well with structured concurrency thinking, even informally: start the calls a request needs, join them, cancel what is left when one fails decisively. Timeouts become simple again.
4. Where they do not help#
Virtual threads do not create resources. If your database pool has 40 connections, the 41st request waits, exactly as before, except now it waits with cheaper threads. That is better, but it is not more throughput. CPU-bound work gains nothing at all; a virtual thread doing math occupies a carrier thread the whole time.
Backpressure also does not disappear. It moves. With unbounded virtual threads, an overloaded downstream now receives more concurrent load instead of your service shedding it at the thread pool boundary. You need semaphores and bulkheads in the places you used to rely on pool sizes, and you need them deliberately.
5. The pinning trap#
The famous footgun is pinning: a virtual thread that blocks inside a synchronized block, or inside a native call, cannot be unmounted, so it holds its carrier thread hostage. A year on, the practical rules are simple. Prefer ReentrantLock where the lock is held across blocking calls, watch for old libraries that synchronize around I/O, and run with the pinning trace turned on in staging so you find them before production does. The JDK keeps improving here, but libraries age slower than runtimes.
6. My defaults now#
For new I/O bound services, I start with virtual threads and blocking code, sized pools for the actual scarce resources, and explicit concurrency limits at service boundaries. I reach for reactive streams when the problem is genuinely a stream: continuous data, fine-grained flow control, complex merging. Both models are tools. The win of the last year is that the simple tool is back on the table.
