Quick revision
The short answer
Parallel streams run on the fork-join common pool, which has only as many workers as CPU cores minus one and is shared by every request in the JVM. Blocking HTTP calls hold those few workers while they wait on the network, so concurrent requests queue behind each other and other users of the pool stall, which makes latency worse under load. Instead, we would run the calls on an executor sized for I/O, usually virtual threads, limit concurrency per downstream with a semaphore or bulkhead, add timeouts, and prove the result with a load test.
What to remember
In this answer, we saw that parallelStream runs on the common pool, which is sized for CPU work and shared by the whole JVM. Blocking HTTP calls hold its few workers, so one request gets faster on a quiet machine while concurrent requests queue behind each other and starve other pool users. The better design runs the calls on virtual threads or a dedicated I/O executor, caps concurrency per downstream with a semaphore or bulkhead, adds timeouts, looks for a batch endpoint or cache first, and is verified with a load test rather than a single request.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Parallel streams run on the fork-join common pool, which has only as many workers as CPU cores minus one and is shared by every request in the JVM. Blocking HTTP calls hold those few workers while they wait on the network, so concurrent requests queue behind each other and other users of the pool stall, which makes latency worse under load. Instead, we would run the calls on an executor sized for I/O, usually virtual threads, limit concurrency per downstream with a semaphore or bulkhead, add timeouts, and prove the result with a load test.
Why this question is asked
This scenario is a classic because the change looks harmless and even passes a quick manual check: one request on a quiet laptop gets faster. The trouble only appears when real traffic arrives. Interviewers use it to see whether we know what parallel streams are designed for, where their threads come from, and how to fan out I/O safely. In this answer, we will recap the endpoint and the common pool, do the arithmetic for one request and for many, see why things get worse under load, and then build the replacement step by step, including how to verify it.
1A Quick Recap of the Situation
The endpoint receives a list of 200 items and, for each one, calls another service over HTTP. Say each call takes about 100 milliseconds. Done one after another, that is 200 x 100 ms = 20 seconds, which is clearly too slow, so the developer wrote items.parallelStream().map(client::fetch).toList().
A parallel stream runs its tasks in ForkJoinPool.commonPool(). That pool has a fixed number of workers, the number of available processors minus one, plus the calling thread, which helps out. It is designed for CPU-bound work: tasks that keep a core busy computing. And it is shared by the whole JVM: every parallel stream, and every CompletableFuture.supplyAsync without an explicit executor, uses the same workers.
2The Arithmetic for One Request and for Many
On an 8-core server the common pool has 7 workers. For a single request, the 200 calls are spread across 7 workers plus the request thread, so 8 calls run at a time: 200 / 8 = 25 rounds of 100 ms, about 2.5 seconds. That is the improvement the developer saw.
Now send 20 requests at once. Each request thread still helps with its own stream, but the 7 shared workers are divided among all 20 requests. On average each request gets its own thread plus about a third of a worker, so roughly 1.35 calls in flight: 200 / 1.35 x 100 ms is close to 15 seconds. Meanwhile, every task in the common pool is a blocked network wait, so anything else that needs the pool, such as an async notification or a CPU-bound parallel stream in another feature, waits behind them too.
| Approach | 1 request | 20 concurrent requests |
|---|---|---|
| Sequential stream | about 20 s | about 20 s each |
parallelStream (7 workers + caller) | about 2.5 s | about 15 s, and the shared pool is blocked |
| Virtual threads, shared limit of 100 calls | about 0.2 s | about 4 s, set by the downstream limit |
3Why Latency Gets Worse Under Load
Three things combine. First, the pool is tiny for I/O. Waiting on a socket uses no CPU, so a pool sized to the core count leaves the machine idle while requests wait. Second, the pool is shared, so concurrent requests are not isolated: they all queue in the same work queues, and one heavy request delays the others. Third, the pool does not add a thread when a worker blocks on ordinary I/O, so nothing relieves the pressure.
Notice that the request threads themselves, typically Tomcat's 200 threads, are also tied up the whole time, waiting for their streams to finish. So the server can accept fewer new requests, and the queue in front of it grows too. That is why a change that made one request faster made the system slower.
4Fan Out on Virtual Threads With a Limit
The fix is to run the blocking calls on threads meant for waiting. On Java 21 and later, virtual threads are the natural choice: they are cheap, so we can have one per call, and a blocked virtual thread does not hold an operating-system thread. But unlimited concurrency simply moves the problem to the downstream service, so we add a limit shared by all requests.
6 messages in order. Play them, or step through them one at a time.
Virtual threads make waiting cheap; the semaphore protects the downstream service.
Notice the three separate controls. The executor gives each call its own cheap thread. The semaphore caps how many calls are in flight to the price service at once, across the whole application, which is a simple bulkhead; Resilience4j's Bulkhead does the same with metrics. The timeouts, both on the HTTP client and with orTimeout, stop one slow call from holding a request forever. The value 100 is not a guess we leave in place: it should come from the downstream team's capacity and from the size of our HTTP connection pool, because extra concurrency beyond the connection pool only queues inside the client. Under heavy load the requests then wait for the downstream's capacity, as in the table above, which is the honest limit of the system rather than an accident of our core count.
Structured concurrency, the StructuredTaskScope API, expresses the same fan-out with automatic cancellation when one call fails. It is still a preview API in Java 25, so check your version and your team's policy on preview features before using it.
5Ask Whether 200 Calls Are Needed at All
Before tuning concurrency, it is worth asking the cheaper question. If the price service offers a batch endpoint, one call with 200 ids replaces 200 round trips. If prices change rarely, a cache with a short time-to-live can serve most items without any call. Either change can beat the best fan-out by an order of magnitude and reduces load on the other team's service.
6Prove It With a Load Test
The original mistake was judged on a single request, so the fix must be judged under realistic load. A load-testing tool such as Gatling, k6 or JMeter can send, for example, 20 to 50 concurrent requests for several minutes against a test environment with a realistic or simulated downstream. We compare p50, p95 and p99 latency, error rates and the downstream's request rate for the old and new versions.
During the test we also watch the JVM: the number of live threads, the semaphore's queue, the HTTP client's connection pool, and, if any parallel streams remain elsewhere, ForkJoinPool.commonPool().getQueuedSubmissionCount(). A fix is proven when latency stays flat as concurrency rises and the downstream service stays within the limits its owners agreed.
7Follow-up questions
Why not just raise the common pool size to 200?
Because the system property java.util.concurrent.ForkJoinPool.common.parallelism changes the pool for the whole JVM. Every CPU-bound parallel stream would then oversubscribe the cores, and all users still share one pool, so the isolation problem remains. Fixing one endpoint should not change the threading of every other feature.
Would a fixed thread pool of platform threads also work?
Yes. Before virtual threads, a dedicated ThreadPoolExecutor of, say, 50 threads for this downstream was the standard answer, and it doubles as a bulkhead. It costs more memory per thread and must be sized and monitored, but it is isolated from the common pool, which is the main thing.
What should happen if some of the 200 calls fail?
That is a product decision we should make explicitly. We can fail the whole request, return partial results with the failed items marked, or retry each call once with a short backoff. Retries must stay inside the overall timeout and within the downstream's limits, or they multiply load exactly when the service is struggling.
8Conclusion
In this answer, we saw that parallelStream runs on the common pool, which is sized for CPU work and shared by the whole JVM. Blocking HTTP calls hold its few workers, so one request gets faster on a quiet machine while concurrent requests queue behind each other and starve other pool users. The better design runs the calls on virtual threads or a dedicated I/O executor, caps concurrency per downstream with a semaphore or bulkhead, adds timeouts, looks for a batch endpoint or cache first, and is verified with a load test rather than a single request.
Open with
The problem is that parallel streams are built for CPU-bound work. They run on the fork-join common pool, which has cores minus one workers plus the caller, so on an 8-core box about 8 calls run at a time.
Then, if they want more
For one request that looks great, 20 seconds down to 2.5. But that pool is shared by the entire JVM. Under load, 20 concurrent requests divide the same 7 workers, every worker sits blocked on the network, and the pool does not add threads for ordinary blocking I/O.
So requests queue behind each other, and anything else using the common pool, like CompletableFuture without an executor, stalls too. Latency gets worse, not better. What I would do: first check whether the downstream has a batch endpoint or whether caching helps.
If we really need 200 calls, run them on an executor meant for I/O, today usually Executors.newVirtualThreadPerTaskExecutor with CompletableFuture, and put a semaphore or Resilience4j bulkhead in front of the downstream so the whole application never exceeds the concurrency that service and our connection pool can handle.
Add client timeouts and orTimeout, decide what partial failure means, and then prove it with a load test at realistic concurrency, comparing p95 and p99, not a single request on a laptop.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21, Java 25
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.stream, package documentation, java.util.function, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A developer changes stream() to parallelStream() on a list of 200 items, each requiring...": https://scrivio-interview-library.netlify.app/java/streams/6b73d0a33e/