Java Reference
In-Depth Information
Parallel Stream Operations
Making an operation execute in parallel using the streams library is a matter of changing a
single method call. If you already have a
Stream
object, then you can call its
parallel
method in order to make it parallel. If you're creating a
Stream
from a
Collection
, you can
call the
parallelStream
method in order to create a parallel stream from the get-go.
Let's look at a simple example in order to make things concrete.
Example 6-1
calculates the
total length of a sequence of albums. It transforms each album into its component tracks,
then gets into the length of each track, and then sums them.
Example 6-1. Serial summing of album track lengths
public
public
int
int
serialArraySum
() {
return
return
albums
.
stream
()
.
flatMap
(
Album:
:
getTracks
)
.
mapToInt
(
Track:
:
getLength
)
.
sum
();
}
of the code is identical. Going parallel
just works
.
Example 6-2. Parallel summing of album track lengths
public
public
int
int
parallelArraySum
() {
return
return
albums
.
parallelStream
()
.
flatMap
(
Album:
:
getTracks
)
.
mapToInt
(
Track:
:
getLength
)
.
sum
();
}
I know the immediate instinct upon hearing this is to go out and replace every call to
stream
with a call to
parallelStream
because it's so easy. Hold your horses for a moment! Obvi-
ously it's important to make good use of parallelism in order to get the most from your hard-
ware, but the kind of data parallelism we get from the streams library is only one form.
The question we really want to ask ourselves is whether it's faster to run our
Stream
-based
code sequentially or in parallel, and that's not a question with an easy answer. If we look
back at the previous example, where we figure out the total running time of a list of albums,
depending upon the circumstances we can make the sequential or parallel versions faster.