J.VI · Java 25 practice question
A backend service can handle at most 20 simultaneous calls to a downstream payment gateway, and the application uses virtual threads. What is the recommended way to enforce that concurrency limit in Java 25?
Check answer and explanation
Correct answer: Use a concurrency-limiting construct such as a Semaphore rather than pooling virtual threads.
Explanation
Using a concurrency-limiting construct such as a Semaphore is the right approach because virtual threads are not meant to be pooled to cap resource access.
Common trap
Creating a fixed-size pool of 20 virtual threads is tempting because developers routinely used thread pools for throttling with platform threads, but JEP 444 explicitly advises against pooling virtual threads for this purpose. Capping carrier threads to 20 is tempting as a global limit, but it restricts unrelated virtual threads across the JVM instead of guarding the specific resource.
Source: JEP 444: Virtual Threads — Description
Checked against the cited text on 2026-11-25.