🔹 What Is Parallel Processing?
Parallel processing is a technique that divides a task into subtasks and executes them concurrently using multiple threads (usually across multiple CPU cores).
Java 8 introduced the parallelStream()
method that enables parallelism in Streams with very little effort.
🔹 Key Features
Feature | Description |
---|---|
Based on Fork/Join framework | Uses ForkJoinPool.commonPool() under the hood |
Splits work into chunks | Breaks the source into substreams and processes them in parallel |
Requires no thread handling | You don’t manage threads yourself — Java handles it |
Designed for CPU-bound tasks | Works well with compute-heavy, stateless, non-blocking operations |
🔹 How to Use
✅ Convert a stream to parallel
List<Integer> numbers = List.of(1, 2, 3, 4, 5);
int sum = numbers.parallelStream()
.mapToInt(i -> i * 2)
.sum();
System.out.println(sum);
🔹 When to Use parallelStream()
✅ Good fit when:
- Data set is large (e.g. millions of elements)
- Computation per element is heavy
- Operations are stateless and non-blocking
- The order of elements doesn’t matter (unless you’re using
forEachOrdered
)
❌ Avoid when:
- Small data set (overhead > performance gain)
- Code is I/O bound or shared-state (like logging or writing to files)
- You care about deterministic order unless explicitly handling it
🔹 Performance Tip: Watch Out!
- parallelStream() != always faster
There’s an overhead in splitting, synchronizing, and merging. - Use tools like
System.nanoTime()
to measure real performance gains. - Avoid shared mutable state — you’ll run into thread-safety issues.
🔹 Customizing Threads (Optional)
ForkJoinPool customPool = new ForkJoinPool(4);
customPool.submit(() ->
list.parallelStream().forEach(MyClass::process)
).get(); // don't forget to handle exceptions
🔹 Common Pitfall
List<Integer> list = Arrays.asList(1, 2, 3, 4);
list.parallelStream().forEach(System.out::println); // ⚠️ Order not guaranteed!
Use .forEachOrdered()
if you need to preserve order:
list.parallelStream().forEachOrdered(System.out::println);
✅ Summary
Concept | Description |
---|---|
parallelStream() | Enables automatic multi-threaded processing |
Best for | Heavy, CPU-bound, stateless operations on big data |
Avoid with | Small data, shared state, or I/O-bound operations |