24

Virtual threads: concurrent carrier fan-out

Fanning a rate quote out across many carriers on virtual threads, bounded and unbounded, with the pinning and pooling caveats.

Demo 24Source on GitHub ↗

Virtual threads make high-concurrency blocking I/O cheap. This module fans a rate quote out across many carriers, each a blocking call, on a newVirtualThreadPerTaskExecutor() (one virtual thread per task), with both an unbounded fan-out and a Semaphore-bounded one that caps concurrent calls while keeping one virtual thread per task. The chapter covers what virtual threads are good for (blocking I/O fan-out) and what they are not (CPU-bound work, pooling them, ThreadLocal at scale), accurately for Java 25 (synchronized no longer pins the carrier; the remaining pinning causes are named).

The JUnit 5 tests prove concurrency with CyclicBarrier rendezvous (not timing), and that the bounded form never exceeds its cap.

Build, run, observe

cd examples/24-virtual-threads
mvn -q test
./demo.sh

The module also runs from the aggregator root with mvn -q -f examples/pom.xml test.