Java / Java 21 Virtual Threads Interview questions
Why is the number of carrier threads limited by default?
By default, the virtual thread scheduler's parallelism equals Runtime.availableProcessors() - the number of CPU cores visible to the JVM.
Carrier threads are the ones that actually execute CPU instructions on behalf of virtual threads, so running more of them than you have cores doesn't add real throughput for CPU-bound segments of code; it just adds context-switching overhead.
Virtual threads scale I/O-bound concurrency, not raw CPU parallelism - the number of things that can be simultaneously waiting is huge, but the number that can be simultaneously computing is still bounded by the hardware.
More Related questions...