Java Reference
In-Depth Information
Previously, when calling
reduce
our initial element could be any value, but for this operation
to work correctly in parallel, it needs to be the
identity
value of the combining function. The
identity value leaves all other elements the same when reduced with them. For example, if
we're summing elements with our
reduce
operation, the combining function is
(acc, ele-
ment) -> acc + element
. The initial element must be
0
, because any number
x
added to
0
returns
x
.
The other caveat specific to
reduce
is that the combining function must be
associative
. This
means that the order in which the combining function is applied doesn't matter as long the
values of the sequence aren't changed. Confused? Don't worry! Take a look at
Example 6-5
,
which shows how we can rearrange the order in which we apply + and
*
to a sequence of
values and get the same result.
Example 6-5. + and * are associative
(
4
+
2
) +
1
=
4
+ (
2
+
1
) =
7
(
4
*
2
) *
1
=
4
* (
2
*
1
) =
8
One thing to avoid is trying to hold locks. The streams framework deals with any necessary
synchronization itself, so there's no need to lock your data structures. If you do try to hold
locks on any data structure that the streams library is using, such as the source collection of
an operation, you're likely to run into trouble.
I explained earlier that you could convert any existing
Stream
to be a parallel stream using
the
parallel
method call. If you've been looking at the API itself while reading the topic,
you may have noticed a
sequential
method as well. When a stream pipeline is evaluated,
there is no mixed mode: the orientation is either parallel or sequential. If a pipeline has calls
to both
parallel
and
sequential
, the last call wins.
Performance
I briefly mentioned before that there were a number of factors that influenced whether paral-
lel streams were faster or slower than sequential streams; let's take a look at those factors
now. Understanding what works well and what doesn't will help you to make an informed
decision about how and when to use parallel streams. There are five important factors that in-
fluence parallel streams performance that we'll be looking at: