Virtual threads and structured concurrency question 115 of 180 mid
Should you pool virtual threads?
- Yes, pooling is always good practice
- Correct: No: they are cheap to create and meant to be one per task, and pooling reintroduces exactly the limit they exist to remove
- Yes, with a pool size of 1000
- Only for CPU-bound work
A pool exists to amortise the cost of an expensive resource. A virtual thread is not expensive, so a
pool of them adds a queue, a lifetime that outlives the task and all the ThreadLocal leakage problems
that come with reuse, while capping concurrency at exactly the number the pool was sized to.
The intended shape is Executors.newVirtualThreadPerTaskExecutor(), which despite the name is not a
pool at all: it starts a fresh virtual thread per task.
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
for (var request : requests) {
executor.submit(() -> handle(request)); // one thread each, thousands of them
}
} // close() waits for them all
If you need to limit concurrency, limit it directly with a Semaphore, which expresses the actual
constraint (the downstream service tolerates 50 in flight) rather than hiding it in a pool size.
Quiz yourself on Virtual threads and structured concurrency Suggest a correction