Java Concurrency Interview

Virtual threads and structured concurrency question 115 of 180 mid

Should you pool virtual threads?

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.

virtual-threads executors

Quiz yourself on Virtual threads and structured concurrency Suggest a correction