Common Pitfalls
Ordering, boxing, and the common pool.
Common Pitfalls is a free Java Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Parallel Is Not Free Speed
Adding parallel() can make code slower, not faster. This lesson covers the classic traps so you can avoid them.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
long sum = IntStream.rangeClosed(1, 1_000_000).parallel()
.asLongStream().sum();
System.out.println(sum);
}
}Pitfall: Ordering Overhead
Ordered operations like forEachOrdered or limit force coordination across threads, eroding parallel gains.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
IntStream.rangeClosed(1, 5).parallel()
.forEachOrdered(System.out::println);
}
}unordered Can Help
If order does not matter, calling unordered() lets the runtime skip ordering work and parallelize more freely.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
long count = IntStream.rangeClosed(1, 1_000_000).parallel()
.unordered()
.filter(n -> n % 5 == 0)
.count();
System.out.println(count);
}
}Pitfall: Hidden Boxing
Using Stream<Integer> in parallel multiplies boxing cost across threads. Prefer primitive streams for numeric parallel work.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
long sum = IntStream.rangeClosed(1, 1_000_000).parallel().asLongStream().sum();
System.out.println(sum);
}
}Pitfall: The Shared Common Pool
All parallel streams share one ForkJoinPool.commonPool(). A long parallel task can starve others in the same JVM, including other parts of your app.
import java.util.concurrent.ForkJoinPool;
public class Main {
public static void main(String[] args) {
System.out.println(ForkJoinPool.commonPool().getParallelism());
}
}Isolating Work in a Custom Pool
To keep heavy work off the common pool, submit the parallel stream inside your own ForkJoinPool.
import java.util.concurrent.ForkJoinPool;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) throws Exception {
ForkJoinPool pool = new ForkJoinPool(2);
long sum = pool.submit(() ->
IntStream.rangeClosed(1, 1_000_000).parallel().asLongStream().sum()
).get();
pool.shutdown();
System.out.println(sum);
}
}Pitfall: Poorly Splittable Sources
LinkedList, Stream.iterate, and BufferedReader.lines split badly. Parallelizing them often yields little or negative benefit.
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
long n = Stream.iterate(1, x -> x + 1)
.limit(100_000)
.parallel()
.filter(x -> x % 2 == 0)
.count();
System.out.println(n);
}
}Pitfall: Stateful Lambdas
Lambdas that read or write external state behave nondeterministically in parallel. Keep them stateless to get consistent results.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
int sum = IntStream.rangeClosed(1, 100).parallel()
.map(n -> n + 1)
.sum();
System.out.println(sum);
}
}Pitfall: findFirst vs findAny
findFirst must respect order, adding overhead in parallel. If any matching element will do, use findAny.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
int found = IntStream.rangeClosed(1, 1_000_000).parallel()
.filter(n -> n % 99991 == 0)
.findAny()
.getAsInt();
System.out.println(found);
}
}Pitfall: Cheap Operations on Small Data
For tiny collections or trivial work, the split-and-merge overhead dwarfs any gain. Stay sequential.
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> small = List.of(1, 2, 3, 4);
int sum = small.stream().mapToInt(Integer::intValue).sum();
System.out.println(sum);
}
}Pitfall: Expensive Merge in Collectors
Collectors with costly merge steps, such as building large sorted structures, can negate parallel gains. Simple accumulations merge cheaply.
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
long total = IntStream.rangeClosed(1, 1_000_000).parallel()
.mapToLong(n -> n)
.sum();
System.out.println(total);
}
}Quick Check
Why can findFirst be slower than findAny on a parallel stream?
Recap
You learned the common parallel pitfalls:
- Ordering (forEachOrdered, limit, findFirst) adds coordination cost; use
unorderedorfindAnywhen possible. - Boxing multiplies cost; prefer primitive streams.
- The shared common pool can starve; isolate heavy work in a custom
ForkJoinPool. - Poorly splittable sources, small data, and expensive merges all undermine parallelism.
Frequently asked questions
Is the “Common Pitfalls” lesson free?
Yes — the full text of “Common Pitfalls” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Common Pitfalls”?
Ordering, boxing, and the common pool. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Common Pitfalls” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.