Java Reference
In-Depth Information
. mapToInt ( i -> i )
. sum ();
Under the hood, parallel streams back onto the fork/join framework. The fork stage recurs-
ively splits up a problem. Then each chunk is operated upon in parallel. Finally, the join
stage merges the results back together.
Figure 6-2 shows how this might apply to Example 6-6 .
Figure 6-2. Decomposing and merging using fork/join
Let's assume that the streams framework is splitting up our work to operate in parallel on a
four-core machine:
1. Our data source is decomposed into four chunks of elements.
2. We perform leaf computation work in parallel on each thread in Example 6-6 . This in-
volves mapping each Integer to an int and also summing a quarter of the values in
each thread. Ideally, we want to spend as much of our time as possible in leaf compu-
tation work because it's the perfect case for parallelism.
3. We merge the results. In Example 6-6 this is just a sum operation, but it might involve
any kind of reduce , collect , or terminal operation.
Search WWH ::

Custom Search