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 library, 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 parallel 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 book, you might have noticed this chapter with the word parallelism in the title and also Chapter 9, which has concurrency in the title. ...
Get Java 8 Lambdas now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.