Sub topic 7
When virtual threads help and when they do not
This chapter assumes you know Java 21 basics, have a working Spring Boot 3.2+ MVC application, and understand blocking I/O such as JDBC and HTTP clients. You will learn how to judge whether a service gains anything from spring.threads.virtual.enabled=true.
What virtual threads actually change
A virtual thread is cheap to create and cheap to block. When it waits on I/O, the JVM unmounts it from its carrier thread, and the carrier runs other virtual threads. This removes the cost of waiting. It does nothing to speed up computing.
So the benefit depends on how a request spends its time. Under load, a platform-thread pool of 200 threads can serve only 200 requests at once, even if each one is idle waiting for a database. Virtual threads remove that cap on concurrency. They do not make a single request faster.
Classify the workload
Look at where a typical request spends its wall-clock time.
| Workload | Time is spent on | Effect of virtual threads |
|---|---|---|
| Calls to JDBC, REST clients, message brokers | Waiting for a response | Higher throughput under concurrent load |
| Fan-out to several slow services | Waiting, many times per request | Higher throughput, simpler code |
| Image processing, hashing, JSON-heavy transforms | CPU | No gain |
| Short requests at low traffic | Little of either | No visible gain |
CPU-bound work is limited by the number of cores. Adding more threads, virtual or not, only adds scheduling overhead. The carrier pool defaults to one carrier thread per available core, so a CPU-heavy task simply holds a carrier until it finishes.
Measure before you decide
Do not guess. Two quick signals tell you whether the thread pool is the limit.
- Under load, Tomcat's busy threads stay at the maximum (200 by default) while CPU usage stays low.
- Request latency climbs with traffic, but the time spent inside each downstream call stays flat. The extra time is queueing for a thread.
If CPU is already near 100%, more concurrency will not help. If threads are mostly busy waiting, it will.
Verify the setting took effect
After you enable the property, confirm requests run on virtual threads. This endpoint reports it.
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
class ThreadInfoController {
@GetMapping("/thread")
String thread() {
Thread current = Thread.currentThread();
return "virtual=" + current.isVirtual() + ", thread=" + current;
}
}With spring.threads.virtual.enabled=true the response should start with virtual=true. If it says false, check that you run Java 21 or newer and Spring Boot 3.2 or newer.
Common mistakes in judging fit
- Expecting lower latency. A single request that waits 300 ms on a database still waits 300 ms. Gains show up as throughput and as lower tail latency under heavy load.
- Moving a CPU-bound service. The code still works, but you gain nothing, and you add risk for no reason.
- Ignoring the downstream limit. If the bottleneck is a database with 10 connections, removing the thread cap just moves the queue to the connection pool. The next chapter covers how to limit that.
- Assuming all blocking code is fine. Blocking inside
synchronizedcan pin a carrier thread on Java 21, which reduces the benefit. A later chapter covers pinning.
Exercise
A service runs on Tomcat with 200 threads. Under load, all 200 threads are busy, CPU usage is 15%, and each request spends most of its time waiting on a PostgreSQL query. The database pool has 20 connections. Will enabling virtual threads help, and what should you watch for?
Show answer
It will likely help the web tier, because the 200-thread cap is the visible limit and the work is I/O-bound. But the 20-connection database pool is the real downstream limit. With virtual threads, thousands of requests can queue for those 20 connections, so you should watch connection wait time and consider limiting concurrency, for example with a Semaphore, so the overload does not move to the database.
Exercise
A service spends 90% of each request resizing images on the CPU. Would you expect higher throughput from virtual threads? Why?
Show answer
No. The work is CPU-bound, so the number of cores sets the limit. Virtual threads only help when threads spend time waiting, because the JVM can then reuse the carrier thread. Here the carrier is never idle.