.parallel() to a stream because the loop is slow and the machine has idle cores. Sometimes the program gets twice as fast. Sometimes it gets ten times slower, and nothing in the code tells you which one you are getting. This post measures both outcomes on a real JDK, finds the line between them, and then covers the two failures that are not about speed at all: one parallel stream starving every other one in the JVM, and parallel streams that quietly return the wrong answer.
You do not need to know anything about fork/join or JMH to follow it. The sections run in difficulty order, each opens with the plain-language version, and the collapsible “going deeper” panels hold the intermediate and reference-level material, so you can read straight through and open them later. Every number comes from a run on JDK 25 whose output is committed to the companion repository, and each code or output block links to the file it came from.
Versions this was built and run against. JDK 25.0.5+8 (Temurin, LTS), JMH 1.37 — the latest GA release on Maven Central, announced on 3 August 2023 — Maven 3.9.11, JUnit Jupiter 5.11.0. Host: a shared 2-vCPU sandbox, so the speed-ups top out near 2x and the numbers show shape, not three significant digits. JMH was run light (1 fork, 3 warmup and 5 measured iterations of one second) to keep the capture short, and every table below carries its error column; I only draw conclusions from gaps larger than it, and say so where the bars overlap.
A parallel stream splits the work, runs the pieces on one shared pool, and merges the answers
Calling.parallel() does not make your lambda faster. It changes who runs it and how many pieces it is cut into. The stream library halves the source repeatedly until the pieces are small, hands each piece to a pool of worker threads as a task, and combines the partial results on the way back up. The calling thread joins in and does a share of the work too.
That pool is one object for the whole JVM: ForkJoinPool.commonPool(). Its default size is the number of CPUs minus one. On the host used here that is one worker, which the captured environment file confirms:
availableProcessors = 2 (default commonPool parallelism = 1)
The code difference between a sequential and a parallel stream is one method call. This is the pair the first benchmark compares (from NqBenchmark.java):
@Benchmark
public long sequential() {
return IntStream.range(0, n).map(i -> work(i, q)).sum();
}
@Benchmark
public long parallel() {
return IntStream.range(0, n).parallel().map(i -> work(i, q)).sum();
}
static int work(int i, long q) {
Blackhole.consumeCPU(q);
return i & 1;
}
Going deeper: how the pieces are sized, read from the JDK 25 bytecode
The stream framework stops splitting when a piece is smaller than roughlysize / (parallelism × 4). I confirmed the shape of that rule by running javap -c on java.util.stream.AbstractTask from JDK 25.0.5: getLeafTarget() returns the pool’s parallelism shifted left by two when the current thread is a ForkJoinWorkerThread, and the common pool’s parallelism shifted left by two otherwise. So with the default parallelism of 1 you still get about four pieces, and a stream started from inside a worker of a different pool is sized by that pool instead. That second sentence is why the private-pool fix in the common-pool section changes anything.
Splitting itself is delegated to the source’s Spliterator, whose trySplit() either returns a cheap half or does real work. The “source” section below measures exactly that difference.
- java.util.stream package documentation — the “Parallelism” and “Non-interference” sections state the rules your lambdas must follow.
- ForkJoinPool — the common pool, its parallelism property, and how workers are managed.
- 00-environment.txt — CPU count and default common-pool parallelism for the runs below.
Small jobs lose: you pay for splitting before you save anything
The cleanest way to see the overhead is to control the two things that decide it: N, how many elements, and Q, how much CPU work each element costs. NqBenchmark.java burns a calibrated amount of CPU per element withBlackhole.consumeCPU(q), so Q is a dial rather than a guess, and runs the same stream sequentially and in parallel at three sizes and two costs.
Benchmark (n) (q) Mode Cnt Score Error Units
NqBenchmark.parallel 100 1 avgt 5 3.227 ± 0.227 us/op
NqBenchmark.parallel 100 100 avgt 5 17.024 ± 5.796 us/op
NqBenchmark.parallel 10000 1 avgt 5 45.297 ± 1.633 us/op
NqBenchmark.parallel 10000 100 avgt 5 688.996 ± 41.521 us/op
NqBenchmark.parallel 1000000 1 avgt 5 1952.430 ± 781.640 us/op
NqBenchmark.parallel 1000000 100 avgt 5 66180.327 ± 5356.241 us/op
NqBenchmark.sequential 100 1 avgt 5 0.345 ± 0.020 us/op
NqBenchmark.sequential 100 100 avgt 5 12.986 ± 0.719 us/op
NqBenchmark.sequential 10000 1 avgt 5 32.956 ± 8.808 us/op
NqBenchmark.sequential 10000 100 avgt 5 1305.048 ± 48.775 us/op
NqBenchmark.sequential 1000000 1 avgt 5 3172.715 ± 886.464 us/op
NqBenchmark.sequential 1000000 100 avgt 5 127480.495 ± 10673.912 us/opQuoted from 03-nq-benchmark.txt.
The rule this grid supports. Compare the total sequential time of the whole terminal operation, not N alone and not the cost of one lambda call. On this host every case whose sequential run took 33 µs or less lost in parallel, and the cases at 1.3 ms or more came out ahead (the 3.2 ms one only narrowly, as noted above). The grid does not resolve the crossover between those two, and on a machine with more cores or a different load it will sit somewhere else. The only way to know is to measure your own pipeline.
Going deeper: finding Q for your own lambda
To place your pipeline on this grid, time it sequentially with JMH and divide by N: that gives you per-element cost, which is your Q. If the whole sequential run is in the tens of microseconds, parallel has nothing to win. If it is in the milliseconds and the elements are independent, parallel is worth a benchmark.Blackhole.consumeCPU(tokens) is JMH’s way of producing a known amount of CPU work the JIT cannot delete; its cost is linear in the token count, which is what makes it usable as a dial. It stands in for a real lambda here, so absolute times will differ from your code. The shape (a flat loss region, then a rise to the core count) should not.
- JMH Tutorial: Writing Java Microbenchmarks That Don’t Lie — warmup, forks,
Blackholeand why the error column matters. - NqBenchmark.java — change
@Paramvalues to put your own N and Q on the grid.
The data source decides whether splitting is cheap
A parallel stream starts by asking its source to split in half. Some sources answer instantly; others have to do real work first. SourceBenchmark.java runs identical per-element work over four sources (100,000 elements each) so that only the source changes. The four parallel/sequential pairs:Benchmark Mode Cnt Score Error Units
SourceBenchmark.arrayList_par avgt 5 864.751 ± 215.142 us/op
SourceBenchmark.arrayList_seq avgt 5 1596.102 ± 137.297 us/op
SourceBenchmark.intArray_par avgt 5 838.116 ± 212.527 us/op
SourceBenchmark.intArray_seq avgt 5 1531.397 ± 308.809 us/op
SourceBenchmark.iterate_par avgt 5 2792.226 ± 115.852 us/op
SourceBenchmark.iterate_seq avgt 5 1962.732 ± 288.329 us/op
SourceBenchmark.linkedList_par avgt 5 1757.232 ± 376.977 us/op
SourceBenchmark.linkedList_seq avgt 5 1665.468 ± 191.938 us/opQuoted from 04-source-benchmark.txt.
int[] or an ArrayList knows its size and can be cut at the midpoint with arithmetic, and both ran about 1.8x and 1.8x faster in parallel. A LinkedList cannot be cut without walking it, and parallel came out 1.1x slower, which given the error columns (377 and 192 µs) is a tie: the splitting cost cancelled the gain. Stream.iterate is the worst case, 1.4x slower in parallel, because every element depends on the one before it, so there is nothing to hand to a second thread until the framework has computed and buffered a batch.
The fix for the last one is rarely clever. Generate the sequence from something splittable instead, such as IntStream.range(0, n) (the source the first benchmark uses), which splits by arithmetic and needs no buffering step. I did not benchmark that substitution separately. The two lines that differ in the benchmark are:
@Benchmark public long arrayList_par() { return arrayList.parallelStream().mapToInt(SourceBenchmark::work).sum(); }
@Benchmark public long iterate_par() { return Stream.iterate(0, i -> i + 1).limit(N).parallel().mapToInt(SourceBenchmark::work).sum(); }
Both are in SourceBenchmark.java.
Going deeper: what the JDK does with an unsplittable source
When a source has no cheap split, the JDK’s fallback spliterators copy elements into an array and hand that array out as a piece. I read the constants from JDK 25.0.5 withjavap -constants: both Spliterators.AbstractSpliterator (what Stream.iterate uses) and LinkedList‘s spliterator start with batches of 1,024 elements and cap them at 33,554,432 (225). The copying is the overhead you see in the table, and it is also why the source has to finish producing a batch before the next thread can start on it.
A source reports what it can promise through characteristics such as SIZED, SUBSIZED and ORDERED. SIZED and SUBSIZED together mean “I know how many elements are in each half”, which is what lets the framework balance the pieces without looking.
- java.util.stream — the section on spliterators and stream sources.
- ArrayList vs LinkedList in 2026: JMH Benchmarks and Why LinkedList Rarely Wins — the same data-structure gap, measured without streams.
- SourceBenchmark.java — the four sources and their eight benchmarks.
Cheap work, boxing, and the benchmark I got wrong
Adding up a million numbers is about as cheap as stream work gets, so it is a natural place to expect parallel to lose, and boxedInteger streams are a natural place to expect it to lose worse. BoxingBenchmark.java measures all four combinations, with -prof gc to report allocation per call.
Benchmark Mode Cnt Score Error Units
BoxingBenchmark.boxed_par avgt 5 857.668 ± 92.144 us/op
BoxingBenchmark.boxed_seq avgt 5 1435.671 ± 131.474 us/op
BoxingBenchmark.primitive_par avgt 5 160.242 ± 22.209 us/op
BoxingBenchmark.primitive_seq avgt 5 269.604 ± 14.943 us/opQuoted from 05-boxing-benchmark.txt.
My expectation was wrong. Parallel won in both cases, about 1.7x for primitives and 1.7x for boxed values. What boxing changed was the absolute price: the boxed sequential pipeline cost 5.3 times the primitive one. The allocation rows add a second fact:BoxingBenchmark.boxed_par:gc.alloc.rate.norm avgt 5 1108.131 ± 4.882 B/op
BoxingBenchmark.boxed_seq:gc.alloc.rate.norm avgt 5 338.076 ± 1.239 B/op
BoxingBenchmark.primitive_par:gc.alloc.rate.norm avgt 5 922.882 ± 8.253 B/op
BoxingBenchmark.primitive_seq:gc.alloc.rate.norm avgt 5 185.884 ± 0.113 B/opQuoted from 05-boxing-benchmark.txt.
A parallel call allocates roughly 737 to 770 more bytes than its sequential twin, which is the task objects of the split tree: the overhead from the first diagram, made visible as memory. The boxed sequential run allocates only 338 bytes per call for a million elements, so the JIT evidently avoided almost all of theInteger allocations. I did not isolate why the boxed pipeline is still 5.3x slower, and I would not guess.
The first draft of this benchmark was unfair, and the numbers looked great. I had written the sequential boxed case withStream.iterateand the parallel boxed case withIntStream.range(..).boxed(). That draft reported 3,399 µs sequential against 783 µs parallel, a “4x speed-up” from.parallel(). Both numbers were real; the comparison was not, because the two pipelines had different sources (the previous section is exactly why that matters). Both cases now use the same source, and the honest speed-up is the 1.7x in the table. That run is not committed; the corrected one is.
Going deeper: primitive streams
IntStream, LongStream and DoubleStream avoid wrapper objects, and the table shows how large that effect can be even when the JIT is doing its best. Convert early with mapToInt / mapToLong and stay primitive through the pipeline; boxed() is a deliberate step back into objects. For a cheap operation the primitive sequential stream is the baseline any parallel version has to beat in absolute time, not only relative to its own boxed sibling.
- BoxingBenchmark.java — the four benchmarks and the comment explaining what each one isolates.
- 05-boxing-benchmark.txt — the full
-prof gcoutput, including the per-iteration allocation rows.
Order is a feature you pay for: limit and distinct
A stream over a list has an encounter order, and by default a parallel stream must give you the same result a sequential one would. Forlimit(5000) that means returning the first 5,000 matches, not just any 5,000, and with the work spread over several threads the pipeline has to track which chunk came first. unordered() tells the framework you do not care. OrderedOpsBenchmark.java measures both, against a sequential baseline:
@Benchmark public long limit_seq() { return data.stream().filter(x -> x % 3 == 0).limit(5_000).count(); }
@Benchmark public long limit_par_ordered() { return data.parallelStream().filter(x -> x % 3 == 0).limit(5_000).count(); }
@Benchmark public long limit_par_unordered() { return data.parallelStream().unordered().filter(x -> x % 3 == 0).limit(5_000).count(); }
From OrderedOpsBenchmark.java; results from 06-ordered-ops-benchmark.txt:
Benchmark Mode Cnt Score Error Units
OrderedOpsBenchmark.distinct_par_ordered avgt 5 9895.062 ± 3221.657 us/op
OrderedOpsBenchmark.distinct_par_unordered avgt 5 6433.784 ± 3429.350 us/op
OrderedOpsBenchmark.distinct_seq avgt 5 4818.155 ± 2083.875 us/op
OrderedOpsBenchmark.limit_par_ordered avgt 5 215.062 ± 26.069 us/op
OrderedOpsBenchmark.limit_par_unordered avgt 5 56.588 ± 8.020 us/op
OrderedOpsBenchmark.limit_seq avgt 5 42.326 ± 39.223 us/op
For limit the effect is large and clear: ordered parallel took 215 µs, 3.8x more than the unordered version (57 µs) and 5.1x more than just running sequentially (42 µs). The unordered parallel run and the sequential one are indistinguishable once you read the error column. distinct is less tidy: ordered parallel was about 2.1x slower than sequential, though the error bars touch, and the unordered run sits between the two with a margin too wide to separate it from either. The documentation says unordered can make distinct cheaper; this run neither confirms nor refutes that.
unordered() changes the answer, not only the speed. With it, limit(5000) returns some 5,000 matching elements, and a different 5,000 on different runs. That is correct when the question is “give me any 5,000 samples” and a bug when it is “give me the first 5,000 orders”. Do not add it to make a benchmark look better.
Going deeper: which operations care about order
Order-sensitive operations includelimit, skip, findFirst, distinct and sorted, and the terminal forEachOrdered. Their relaxed counterparts are findAny and plain forEach, and Collectors.groupingByConcurrent in place of groupingBy. In each case the trade is the same: the relaxed form lets threads finish in any order, and the answer may differ from the sequential one.
- java.util.stream — the “Ordering” section lists which stages preserve or ignore encounter order.
The common pool is shared: one blocked parallel stream slows every other one
Everything so far was about one stream running alone. In a real service, many things use the common pool at once: other parallel streams, andCompletableFuture async methods that were not given an executor (the Javadoc says they default to the common pool). If any of them blocks, for an HTTP call or a database query, it holds a worker thread that no one else can use. CommonPoolStarvationDemo.java stages exactly that. The victim is 16 tasks of 50 ms each; the demo pins the pool to 3 workers so the result is the same on any machine.
availableProcessors = 2
commonPool parallelism = 3
1. Victim alone
victim took 201 ms
2. Victim while a neighbour BLOCKS the common pool (8 tasks x 2000 ms)
victim took 803 ms
3. Same neighbour, but it runs in its OWN ForkJoinPool(3)
victim took 200 ms
4. 20 blocking tasks x 200 ms: parallel stream vs virtual threads
parallel stream : 1002 ms
virtual threads : 216 msWhole file: 02-common-pool-starvation.txt.
ForkJoinPool restores 200 ms. The code for that fix is short:
try (ForkJoinPool pool = new ForkJoinPool(3)) {
pool.submit(() -> IntStream.range(0, 8).parallel().forEach(i -> sleep(2_000))).join();
}
This is the ownPool branch of startNeighbour in CommonPoolStarvationDemo.java.
try (var ex = Executors.newVirtualThreadPerTaskExecutor()) {
for (int i = 0; i < 20; i++) ex.submit(() -> sleep(200));
}
Also from CommonPoolStarvationDemo.java; the executor is closed by the try, which waits for the tasks.
The private-pool trick works, but I would not call it a guarantee. The demo shows that a parallel stream started from inside aForkJoinPooltask runs in that pool on JDK 25.0.5, and the bytecode ofgetLeafTarget()is consistent with it. I did not find it stated as a contract in the stream documentation, so if you rely on it, keep a test like the one that producedoutput/02and re-run it on every JDK upgrade.
Going deeper: why the pool does not rescue you
AForkJoinPool can add a spare thread when a task tells it that it is about to block, through ForkJoinPool.managedBlock(..). Thread.sleep, JDBC and HTTP clients do not do that, so the pool sees a busy worker and nothing more, which is why the neighbour in the demo cannot be worked around from the outside.
Two knobs exist for the common pool itself. The system property java.util.concurrent.ForkJoinPool.common.parallelism sets its size, which the demo uses (the output shows 3 on a 2-CPU host). Raising it for blocking work is a patch, not a design: it enlarges a pool that every parallel stream in the JVM shares, and it does nothing for CPU-bound work.
- ForkJoinPool — the
ManagedBlockermechanism and the common-pool property. - Executors.newVirtualThreadPerTaskExecutor — the executor used above.
- Java 21 Features with Examples: Virtual Threads, Record Patterns, Sequenced Collections — where virtual threads became final.
Parallel streams that return the wrong answer, with no exception
Speed is the forgiving failure. The unforgiving one is a parallel stream that returns a wrong answer and says nothing. CorrectnessBugsDemo.java shows four ways to get there; this is its whole output:1. forEach(list::add), n = 100000, ten runs (-1 = exception):
sizes: 90126 86309 22551 86194 -1 22732 87868 10443 15012 77596
collect(toList()) size: 100000
2. reduce(10, Integer::sum) over 1..100
sequential: 5060
parallel : 5210
3. reduce(0, (a, b) -> a - b) over 1..1000
sequential: -500500
parallel : 0
4. counter[0]++ in forEach, n = 100000
racy : 99997
atomic : 100000From 01-correctness-bugs.txt.
Bug 1,forEach into a shared list. ArrayList is not thread-safe, so concurrent add calls overwrite each other. Ten runs kept somewhere between a tenth and nine-tenths of the 100,000 elements, and one threw (the -1). The fix is to let the stream do the collecting, which returned all 100,000. The two methods are in CorrectnessBugsDemo.java:
public static int sharedListSize(int n) {
List<Integer> out = new ArrayList<>();
try {
IntStream.range(0, n).parallel().forEach(out::add);
} catch (RuntimeException e) {
return -1; // ArrayIndexOutOfBoundsException from a racing resize
}
return out.size();
}
public static int collectedListSize(int n) {
return IntStream.range(0, n).parallel().boxed().collect(Collectors.toList()).size();
}
Bug 2, a reduce identity that is not an identity. reduce(10, Integer::sum) adds the 10 once sequentially, giving 5060. In parallel every leaf starts from its own copy of the identity, so the 10 is added once per piece and the answer was 5210. The contract is that the identity must leave any value unchanged, and 10 does not.
public static int sumWithBadIdentity(boolean parallel) {
var s = IntStream.rangeClosed(1, 100);
return (parallel ? s.parallel() : s).reduce(10, Integer::sum);
}
Bug 3, a non-associative function. Subtraction gives -500500 sequentially and 0 in parallel, because parallel regroups the operations and (a - b) - c is not a - (b - c). Bug 4, a shared counter. counter[0]++ from many threads lost updates; AtomicInteger was exact.
The racy numbers are the symptom, not a result. The 10 list sizes and the counter value above differ every run (an earlier run of the same code, not committed, gave a counter of 96249 instead of 99997). The test class pins only what is deterministic: the collected size, the two reduce results, and the atomic counter. Do not write a test that asserts a race will happen.
Going deeper: the rules your lambdas must follow
The stream documentation asks for three things of any behavioural parameter in a parallel pipeline: it must be non-interfering (it does not modify the source while the stream runs), stateless (its result does not depend on anything that can change during the pipeline), and, for areduce, associative with a true identity. The four bugs above each break one of these. The safe replacement for “add to a list” is collect(Collectors.toList()) or Stream.toList(); for a counter, count() or sum() and not a captured variable.
- java.util.stream — “Reduction operations”, “Associativity” and “Side-effects”.
- Lambda Expressions in Java: Syntax, Method References, Capture and How They Compile — why a lambda can only capture effectively final locals, and what the array trick above gets around.
- ParallelStreamsTest.java — the five deterministic assertions.
So when should you use a parallel stream?
Taken together, the measurements give a short checklist. Reach for.parallel() only when every row of the left column is true.
| Question | Parallel is reasonable if… | Evidence above |
|---|---|---|
| How long does the sequential run take? | Milliseconds or more, not microseconds | N×Q grid: every case at 33 µs or less lost |
| What is the source? | An array, ArrayList or range | LinkedList tied, Stream.iterate lost |
| Does any step need encounter order? | No, or you accept unordered() | limit: ordered parallel several times slower |
| Does any step block? | No. If yes, use virtual threads | Common-pool demo: 201 ms became 803 ms |
| Are the lambdas stateless and associative? | Yes, with a true identity | Four wrong answers, no exception |
| Have you measured on your hardware? | Yes, with JMH, and the gap exceeds the error | Million-cheap-element case: ranges overlapped |
Should you use parallel streams at all? My default is no: write the sequential stream, and add .parallel() only after a measurement shows a CPU-bound pipeline that is long enough to cover the overhead. Inside a web server it carries an extra cost, because every request that uses it competes for the same shared pool, so one slow request can slow an unrelated one. If the real goal is to do many blocking things at once, the right tool is an executor of virtual threads. And since all of these numbers were taken on a shared 2-vCPU host, treat any result that surprises you as a reason to re-run on a quiet machine before you act on it.
Run it yourself
Everything is in theparallel-streams module of java-core-examples. These commands are from the README.md:
export JAVA_HOME=/path/to/jdk-25
mvn -q package # builds target/benchmarks.jar and runs the tests
java -Djava.util.concurrent.ForkJoinPool.common.parallelism=3 \
-cp target/classes com.ankurm.parallelstreams.CorrectnessBugsDemo
java -Djava.util.concurrent.ForkJoinPool.common.parallelism=3 \
-cp target/classes com.ankurm.parallelstreams.CommonPoolStarvationDemo
java -jar target/benchmarks.jar NqBenchmark -f 1 -wi 3 -w 1s -i 5 -r 1s
scripts/run-all.sh (run-all.sh) regenerates every file in output/; it takes about six minutes on two vCPUs. There is deliberately no docs/ folder: the deeper material is in the panels on this page.
Further reading
- java-core-examples / parallel-streams — every file referenced in this post, the captured output and the test class.
- JMH Tutorial: Writing Java Microbenchmarks That Don’t Lie — the benchmarking rules this post relies on.
- ArrayList vs LinkedList in 2026: JMH Benchmarks and Why LinkedList Rarely Wins — the data-structure side of the source section.
- Java 21 Features with Examples — virtual threads, the alternative for blocking work.
- Lambda Expressions in Java — capture rules behind the shared-state bugs.
- java.util.stream package documentation — parallelism, ordering, reduction and side-effect rules.
- ForkJoinPool (JDK 25) — the common pool and managed blocking.
No Comments yet!