Java Reference
In-Depth Information
Consider whether a terminal operation has a cheap or expensive merge step (for example, the
combiner method in a Collector ). If this is expensive, then the cost caused by the combination of the
partial results generated by each substream can outweigh the performance benefits of a parallel
stream.
Table 7.1 gives a summary of the parallel-friendliness of certain stream sources in terms of their
decomposability.
Table 7.1. Stream sources and decomposability
Source
Decomposability
ArrayList
Excellent
LinkedList
Poor
IntStream.range
Excellent
Stream.iterate
Poor
HashSet
Good
TreeSet
Good
Finally, we need to emphasize that the infrastructure used behind the scenes by parallel streams
to execute operations in parallel is the fork/join framework introduced in Java 7. The parallel
summing example proved that it's vital to have a good understanding of the parallel stream
internals in order to use them correctly, so we'll investigate in detail the fork/join framework in
the next section.
7.2. The fork/join framework
The fork/join framework was designed to recursively split a parallelizable task into smaller tasks
and then combine the results of each subtask to produce the overall result. It's an
implementation of the ExecutorService interface, which distributes those subtasks to worker
threads in a thread pool, called ForkJoinPool. Let's start by exploring how to define a task and
subtasks.
 
Search WWH ::




Custom Search