# parallel-streams Companion code for the ankurm.com post *"Java Parallel Streams: When They're Slower (JMH Benchmarks and the Common Pool)."* Module in `java-core-examples`, the Java-core series. There is deliberately no `docs/` folder: the deeper material lives in collapsible sections inside the post itself. ## Versions this was built and tested against | Component | Version | Notes | |---|---|---| | JDK | 25.0.5+8 (Temurin, LTS) | | | JMH | 1.37 | Latest GA on Maven Central; released 3 Aug 2023. | | JUnit Jupiter | 5.11.0 | | | Maven | 3.9.11 | | | Host | 2 vCPU shared sandbox | Default common-pool parallelism is therefore 1 (see `output/00`). Speed-ups cap near 2x. Read the shape of the numbers, not the third significant digit. | ## Quickstart ```bash 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` regenerates every file in `output/` (about 6 minutes on 2 vCPUs). `scripts/run.sh ` runs one demo class ad hoc. ## What's in here | File | What it shows | |---|---| | `NqBenchmark.java` | N elements x Q units of CPU per element, sequential vs parallel, at three sizes and two costs. Where parallel starts to pay. | | `SourceBenchmark.java` | The same work over `int[]`, `ArrayList`, `LinkedList` and `Stream.iterate`: how splittable the source is decides the outcome. | | `BoxingBenchmark.java` | A cheap sum over 1,000,000 elements, primitive vs boxed, with `-prof gc` allocation per call. | | `OrderedOpsBenchmark.java` | `limit` and `distinct`, ordered vs `unordered()`, against a sequential baseline. | | `CommonPoolStarvationDemo.java` | A neighbour that blocks the common pool slows an unrelated parallel stream 4x; a private `ForkJoinPool` and virtual threads as the fixes. | | `CorrectnessBugsDemo.java` | `forEach(list::add)`, a non-identity `reduce` identity, a non-associative accumulator, and a racy counter. | | `ParallelStreamsTest.java` | Pins the deterministic claims. No timing assertions. | | `output/00-07` | Captured runs of all of the above. | ## Notes worth knowing before reading the post - **JMH settings are light** (`-f 1 -wi 3 -i 5`, one second each) to keep the whole capture to minutes. Error bars are reported in the output; the post draws conclusions only from differences clearly larger than them. - **The demos pin `common.parallelism=3`** so they behave the same on any machine. The JMH runs do not: they use the default for the host, which is 1 on 2 CPUs. - **`BoxingBenchmark`'s first draft compared two different stream sources** (`Stream.iterate` for the sequential boxed case, `range().boxed()` for the parallel one). That is not a fair pair; both now use `IntStream.range(..).boxed()`. ## License MIT - see the [repo-wide LICENSE](../LICENSE).