Java Reference
In-Depth Information
Chapter 6. Data Parallelism
I've previously made a lot of references to the idea that it's easier to write parallel code in
Java 8. This is because we can use lambda expressions in combination with the streams lib-
rary, introduced in Chapter 3 , to say what we want our program to do, regardless of whether
it's sequential or parallel. I know that sounds a lot like what you've been doing in Java for
years, but there's a difference between saying what you want to compute and saying how to
compute it.
The big shift between external and internal iteration (also discussed in Chapter 3 ) did make it
easier to write simple and clean code, but here's the other big benefit: now we don't have to
manually control the iteration. It doesn't need to be performed sequentially. We express the
what and, by changing a single method call, we can get a library to figure out the how.
The changes to your code are surprisingly unobtrusive, so the majority of this chapter won't
be talking about how your code changes. Instead, I'll explain why you might want to go par-
allel and when you'll get performance improvements. It's also worth noting that this chapter
isn't a general text on performance in Java; we'll just be looking at the easy wins provided in
Java 8.
Parallelism Versus Concurrency
After a quick scan over the table of contents of this topic, you might have noticed this
chapter with the word parallelism in the title and also Chapter 9 , which has concurrency in
the title. Don't worry—I haven't repeated the same material in an attempt to justify charging
you more for this topic! Concurrency and parallelism are different things that can be lever-
aged to achieve different aims.
Concurrency arises when two tasks are making progress at overlapping time periods. Paral-
lelism arises when two tasks are happening at literally the same time, such as on a multicore
CPU. If a program is undertaking two tasks and they are being given small slices of a single
CPU core's time, then it is exhibiting concurrency but not parallelism. This difference is
shown in Figure 6-1 .
Search WWH ::




Custom Search