17
Overview of Java 8 Parallel Streams (Part 1) Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Professor of Computer Science Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Overview of Java 8 Parallel Streams (Part 1)schmidt/cs891f/2018-PDFs/03-overview-of-Java-8-parallelstreams...4 Transitioning from Sequential Streams to Parallel Streams •A Java 8

  • Upload
    others

  • View
    11

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Overview of Java 8 Parallel Streams (Part 1)schmidt/cs891f/2018-PDFs/03-overview-of-Java-8-parallelstreams...4 Transitioning from Sequential Streams to Parallel Streams •A Java 8

Overview of Java 8 Parallel Streams

(Part 1)

Douglas C. [email protected]

www.dre.vanderbilt.edu/~schmidt

Professor of Computer Science

Institute for Software

Integrated Systems

Vanderbilt University

Nashville, Tennessee, USA

Page 2: Overview of Java 8 Parallel Streams (Part 1)schmidt/cs891f/2018-PDFs/03-overview-of-Java-8-parallelstreams...4 Transitioning from Sequential Streams to Parallel Streams •A Java 8

2

Learning Objectives in this Part of the Lesson• Know how aggregate operations from Java 8

sequential streams are applied in the parallel streams framework

Output

f(x)

Output

g(f(x))

Input x

Intermediate operation (behavior f)

Intermediate operation (behavior g)

Terminal operation (reducer)

Stream factory operation ()

Page 3: Overview of Java 8 Parallel Streams (Part 1)schmidt/cs891f/2018-PDFs/03-overview-of-Java-8-parallelstreams...4 Transitioning from Sequential Streams to Parallel Streams •A Java 8

3

Transitioning from Sequential Streams to

Parallel Streams

Page 4: Overview of Java 8 Parallel Streams (Part 1)schmidt/cs891f/2018-PDFs/03-overview-of-Java-8-parallelstreams...4 Transitioning from Sequential Streams to Parallel Streams •A Java 8

4

Transitioning from Sequential Streams to Parallel Streams• A Java 8 stream is a pipeline of aggregate operations that process a

sequence of elements (aka, “values” or “data”)

Stream<SearchResults>

Stream<String>

Stream<SearchResults>

List<SearchResults>

List<String> …

map(phrase -> searchForPhrase(…))

filter(not(SearchResults::isEmpty))

collect(toList())

Search Phrases

stream()

See github.com/douglascraigschmidt/LiveLessons/tree/master/SearchStreamGang

Page 5: Overview of Java 8 Parallel Streams (Part 1)schmidt/cs891f/2018-PDFs/03-overview-of-Java-8-parallelstreams...4 Transitioning from Sequential Streams to Parallel Streams •A Java 8

5

Transitioning from Sequential Streams to Parallel Streams• A Java 8 stream is a pipeline of aggregate operations that process a

sequence of elements (aka, “values” or “data”)

Stream<SearchResults>

Stream<String>

Stream<SearchResults>

List<SearchResults>

List<String> …

map(phrase -> searchForPhrase(…))

filter(not(SearchResults::isEmpty))

collect(toList())

Search Phrases

stream()

An aggregate operation applies a “behavior” to every element in a stream

Page 6: Overview of Java 8 Parallel Streams (Part 1)schmidt/cs891f/2018-PDFs/03-overview-of-Java-8-parallelstreams...4 Transitioning from Sequential Streams to Parallel Streams •A Java 8

6

Transitioning from Sequential Streams to Parallel Streams• By default, a stream executes sequentially, so all its aggregate operations run

behaviors in a single thread of control

Stream<SearchResults>

Stream<String>

Stream<SearchResults>

List<SearchResults>

List<String> …

map(phrase -> searchForPhrase(…))

filter(not(SearchResults::isEmpty))

collect(toList())

Search Phrases

stream()

Page 7: Overview of Java 8 Parallel Streams (Part 1)schmidt/cs891f/2018-PDFs/03-overview-of-Java-8-parallelstreams...4 Transitioning from Sequential Streams to Parallel Streams •A Java 8

7

• When a stream executes in parallel, it is partitioned into multiple substream“chunks” that run in a common fork-join pool

Stream<SearchResults>

Stream<String>

Stream<SearchResults>

List<SearchResults>

List<String> …

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html

map(phrase -> searchForPhrase(…))

filter(not(SearchResults::isEmpty))

collect(toList())

Search Phrases

parallelStream()

Transitioning from Sequential Streams to Parallel Streams

Page 8: Overview of Java 8 Parallel Streams (Part 1)schmidt/cs891f/2018-PDFs/03-overview-of-Java-8-parallelstreams...4 Transitioning from Sequential Streams to Parallel Streams •A Java 8

8

• When a stream executes in parallel, it is partitioned into multiple substream“chunks” that run in a common fork-join pool

Stream<SearchResults>

Stream<String>

Stream<SearchResults>

List<SearchResults>

List<String> …

Threads in the fork-join pool (non-deterministically) process different chunks

map(phrase -> searchForPhrase(…))

filter(not(SearchResults::isEmpty))

collect(toList())

Search Phrases

parallelStream()

Transitioning from Sequential Streams to Parallel Streams

Page 9: Overview of Java 8 Parallel Streams (Part 1)schmidt/cs891f/2018-PDFs/03-overview-of-Java-8-parallelstreams...4 Transitioning from Sequential Streams to Parallel Streams •A Java 8

9Intermediate operations iterate over & process these chunks in parallel

Stream<SearchResults>

Stream<String>

Stream<SearchResults>

List<SearchResults>

List<String> …

• When a stream executes in parallel, it is partitioned into multiple substream“chunks” that run in a common fork-join pool

map(phrase -> searchForPhrase(…))

filter(not(SearchResults::isEmpty))

collect(toList())

Search Phrases

parallelStream()

Transitioning from Sequential Streams to Parallel Streams

Page 10: Overview of Java 8 Parallel Streams (Part 1)schmidt/cs891f/2018-PDFs/03-overview-of-Java-8-parallelstreams...4 Transitioning from Sequential Streams to Parallel Streams •A Java 8

10

Stream<SearchResults>

Stream<String>

Stream<SearchResults>

List<SearchResults>

List<String> …

• When a stream executes in parallel, it is partitioned into multiple substream“chunks” that run in a common fork-join pool

A terminal operation then combines the chunks into a single result

map(phrase -> searchForPhrase(…))

filter(not(SearchResults::isEmpty))

collect(toList())

Search Phrases

parallelStream()

Transitioning from Sequential Streams to Parallel Streams

Page 11: Overview of Java 8 Parallel Streams (Part 1)schmidt/cs891f/2018-PDFs/03-overview-of-Java-8-parallelstreams...4 Transitioning from Sequential Streams to Parallel Streams •A Java 8

11(Stateless) Java 8 lambda expressions & method references are used to pass behaviors

Stream<SearchResults>

Stream<String>

Stream<SearchResults>

List<SearchResults>

List<String> …

• When a stream executes in parallel, it is partitioned into multiple substream“chunks” that run in a common fork-join pool

map(phrase -> searchForPhrase(…))

filter(not(SearchResults::isEmpty))

collect(toList())

Search Phrases

parallelStream()

Transitioning from Sequential Streams to Parallel Streams

Page 12: Overview of Java 8 Parallel Streams (Part 1)schmidt/cs891f/2018-PDFs/03-overview-of-Java-8-parallelstreams...4 Transitioning from Sequential Streams to Parallel Streams •A Java 8

12

Stream<SearchResults>

Stream<String>

Stream<SearchResults>

List<SearchResults>

List<String> …

• When a stream executes in parallel, it is partitioned into multiple substream“chunks” that run in a common fork-join pool

map(phrase -> searchForPhrase(…))

filter(not(SearchResults::isEmpty))

collect(toList())

Search Phrases

parallelStream() vs. stream()

Transitioning from Sequential Streams to Parallel Streams

Ideally, minuscule changes needed to transition from sequential to parallel stream

Page 13: Overview of Java 8 Parallel Streams (Part 1)schmidt/cs891f/2018-PDFs/03-overview-of-Java-8-parallelstreams...4 Transitioning from Sequential Streams to Parallel Streams •A Java 8

13See docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html

• The same aggregate operations can be used for sequential & parallel streams

Transitioning from Sequential Streams to Parallel Streams

Page 14: Overview of Java 8 Parallel Streams (Part 1)schmidt/cs891f/2018-PDFs/03-overview-of-Java-8-parallelstreams...4 Transitioning from Sequential Streams to Parallel Streams •A Java 8

14

• The same aggregate operations can be used for sequential & parallel streams

See github.com/douglascraigschmidt/LiveLessons/tree/master/SearchStreamGang

e.g., SearchStreamGang uses the same aggregate operations for both SearchWithSequentialStreams& SearchWithParallelStreams implementations

map(phrase -> searchForPhrase(…))

filter(not(SearchResults::isEmpty))

collect(toList())

Search Phrases

stream() vs. parallelStream()

Transitioning from Sequential Streams to Parallel Streams

Page 15: Overview of Java 8 Parallel Streams (Part 1)schmidt/cs891f/2018-PDFs/03-overview-of-Java-8-parallelstreams...4 Transitioning from Sequential Streams to Parallel Streams •A Java 8

15

• The same aggregate operations can be used for sequential & parallel streams

• Java 8 streams can thus treat parallelism as an optimization & leverage all available cores!

See qconlondon.com/london-2017/system/files/presentation-slides/concurrenttoparallel.pdf

map(phrase -> searchForPhrase(…))

filter(not(SearchResults::isEmpty))

collect(toList())

Search Phrases

parallelStream()

Transitioning from Sequential Streams to Parallel Streams

Page 16: Overview of Java 8 Parallel Streams (Part 1)schmidt/cs891f/2018-PDFs/03-overview-of-Java-8-parallelstreams...4 Transitioning from Sequential Streams to Parallel Streams •A Java 8

16

• The same aggregate operations can be used for sequential & parallel streams

• Java 8 streams can thus treat parallelism as an optimization & leverage all available cores!

• Naturally, behaviors run by these aggregate operations must be designed carefully to avoid accessing unsynchronized shared state..

See henrikeichenhardt.blogspot.com/2013/06/why-shared-mutable-state-is-root-of-all.html

map(phrase -> searchForPhrase(…))

filter(not(SearchResults::isEmpty))

collect(toList())

Search Phrases

parallelStream()

Transitioning from Sequential Streams to Parallel Streams

Page 17: Overview of Java 8 Parallel Streams (Part 1)schmidt/cs891f/2018-PDFs/03-overview-of-Java-8-parallelstreams...4 Transitioning from Sequential Streams to Parallel Streams •A Java 8

17

End of Overview of Java 8 Parallel Streams

(Part 1)