195. Parallel processing of streams

In a nutshell, parallel processing a stream refers to a process that consists of three steps:

  1. Splitting the elements of a stream into multiple chunks
  2. Processing each chunk in a separate thread
  3. Joining the results of processing in a single result

These three steps take place behind the scenes via the default ForkJoinPool method as we discussed in Chapter 10, Concurrency – Thread Pools, Callables, and Synchronizers and Chapter 11, Concurrency – Deep Dive.

As a rule of thumb, parallel processing can be applied only to stateless (the state of an element doesn't affect another element), non-interfering (the data source is not affected), and associative (the result is not affected by the order of operands) ...

Get Java Coding Problems now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.