Java Streams: Sequential vs. Parallel Streamsschmidt/cs891f/2019-PDFs/X... · 7 •A parallel...

17
Java Streams: Sequential vs. Parallel Streams Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Professor of Computer Science Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Transcript of Java Streams: Sequential vs. Parallel Streamsschmidt/cs891f/2019-PDFs/X... · 7 •A parallel...

Page 1: Java Streams: Sequential vs. Parallel Streamsschmidt/cs891f/2019-PDFs/X... · 7 •A parallel stream splits its elements into multiple chunk & uses the common fork-join pool to process

Java Streams:

Sequential vs. Parallel Streams

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: Java Streams: Sequential vs. Parallel Streamsschmidt/cs891f/2019-PDFs/X... · 7 •A parallel stream splits its elements into multiple chunk & uses the common fork-join pool to process

2

Learning Objectives in this Part of the Lesson• Understand the structure & functionality

of Java streams, e.g.,

• Fundamentals of streams

• Benefits of streams

• Creating a stream

• Aggregate operations in a stream

• Applying streams in practice

• Sequential vs. parallel streams

Input x

Output f(x)

Output g(f(x))

Stream source

Aggregate operation (behavior f)

Aggregate operation (behavior g)

Aggregate operation (behavior h)

See radar.oreilly.com/2015/02/java-8-streams-api-and-parallelism.html

Page 3: Java Streams: Sequential vs. Parallel Streamsschmidt/cs891f/2019-PDFs/X... · 7 •A parallel stream splits its elements into multiple chunk & uses the common fork-join pool to process

3

Sequential vs. Parallel Streams

Page 4: Java Streams: Sequential vs. Parallel Streamsschmidt/cs891f/2019-PDFs/X... · 7 •A parallel stream splits its elements into multiple chunk & uses the common fork-join pool to process

4

• Stream operations run sequentially

Sequential vs. Parallel Streams

stream()

Aggregate operation (behavior f)

Aggregate operation (behavior g)

See docs.oracle.com/javase/tutorial/collections/streams

We’ll cover sequential streams first

Page 5: Java Streams: Sequential vs. Parallel Streamsschmidt/cs891f/2019-PDFs/X... · 7 •A parallel stream splits its elements into multiple chunk & uses the common fork-join pool to process

5

• Stream operations run sequentially or in parallel

See docs.oracle.com/javase/tutorial/collections/streams/parallelism.html

Sequential vs. Parallel Streams

parallelStream()

Aggregate operation (behavior f)

Aggregate operation (behavior g)We’ll cover parallel streams later

Page 6: Java Streams: Sequential vs. Parallel Streamsschmidt/cs891f/2019-PDFs/X... · 7 •A parallel stream splits its elements into multiple chunk & uses the common fork-join pool to process

6

• A parallel stream splits its elements into multiplechunk & uses the common fork-join pool to process these chunks independently

Sequential vs. Parallel Streams

Aggregate operation (behavior f)

Aggregate operation (behavior g)

Seedzone.com/articles/common-fork-join-pool-and-streams

parallelStream()

Page 7: Java Streams: Sequential vs. Parallel Streamsschmidt/cs891f/2019-PDFs/X... · 7 •A parallel stream splits its elements into multiple chunk & uses the common fork-join pool to process

7

• A parallel stream splits its elements into multiplechunk & uses the common fork-join pool to process these chunks independently

Sequential vs. Parallel Streams

Starting SearchStreamGangTestPARALLEL_SPLITERATOR executed in 409 msecsCOMPLETABLE_FUTURES_INPUTS executed in 426 msecsCOMPLETABLE_FUTURES_PHASES executed in 427 msecsPARALLEL_STREAMS executed in 437 msecsPARALLEL_STREAM_PHASES executed in 440 msecsRXJAVA_PHASES executed in 485 msecsPARALLEL_STREAM_INPUTS executed in 802 msecsRXJAVA_INPUTS executed in 866 msecsSEQUENTIAL_LOOPS executed in 1638 msecsSEQUENTIAL_STREAM executed in 1958 msecsEnding SearchStreamGangTest

Tests conducted on a quad-core Lenovo P50 with 32 Gbytes of RAM

A parallel stream is often more efficient and scalable than a sequential stream.

Aggregate operation (behavior f)

Aggregate operation (behavior g)

parallelStream()

Page 8: Java Streams: Sequential vs. Parallel Streamsschmidt/cs891f/2019-PDFs/X... · 7 •A parallel stream splits its elements into multiple chunk & uses the common fork-join pool to process

8

• Ideally, a behavior’s output in a stream depends only on its input arguments

Aggregate operation (behavior f)

See en.wikipedia.org/wiki/Side_effect_(computer_science)

Input x

Output f(x)

Sequential vs. Parallel Streams…

Page 9: Java Streams: Sequential vs. Parallel Streamsschmidt/cs891f/2019-PDFs/X... · 7 •A parallel stream splits its elements into multiple chunk & uses the common fork-join pool to process

9

• Ideally, a behavior’s output in a stream depends only on its input arguments

Aggregate operation (behavior f)

Output f(x)String capitalize(String s) {

if (s.length() == 0)

return s;

return s.substring(0, 1)

.toUpperCase()

+ s.substring(1)

.toLowerCase();

See github.com/douglascraigschmidt/LiveLessons/tree/master/Java8/ex12

Sequential vs. Parallel Streams…

Input x

Page 10: Java Streams: Sequential vs. Parallel Streamsschmidt/cs891f/2019-PDFs/X... · 7 •A parallel stream splits its elements into multiple chunk & uses the common fork-join pool to process

10

• Ideally, a behavior’s output in a stream depends only on its input arguments

• Behaviors with side-effects can incur race conditions in parallel streams

See en.wikipedia.org/wiki/Race_condition#Software

Race conditions arise in software when an application depends on the sequence or

timing of threads for it to operate properly

Sequential vs. Parallel Streams…

Aggregate operation (behavior f)

Aggregate operation (behavior g)

Aggregate operation (behavior h)

Page 11: Java Streams: Sequential vs. Parallel Streamsschmidt/cs891f/2019-PDFs/X... · 7 •A parallel stream splits its elements into multiple chunk & uses the common fork-join pool to process

11

class Total {

public long mTotal = 1;

public void mult(long n)

{ mTotal *= n; }

}

See github.com/douglascraigschmidt/LiveLessons/tree/master/Java8/ex16

• Ideally, a behavior’s output in a stream depends only on its input arguments

• Behaviors with side-effects can incur race conditions in parallel streams, e.g.

long factorial(long n) {

Total t = new Total();

LongStream

.rangeClosed(1, n)

.parallel()

.forEach(t::mult);

return t.mTotal;

}

Sequential vs. Parallel Streams

A buggy attempt to compute the ‘nth’ factorial in parallel

Page 12: Java Streams: Sequential vs. Parallel Streamsschmidt/cs891f/2019-PDFs/X... · 7 •A parallel stream splits its elements into multiple chunk & uses the common fork-join pool to process

12

class Total {

public long mTotal = 1;

public void mult(long n)

{ mTotal *= n; }

} long factorial(long n) {

Total t = new Total();

LongStream

.rangeClosed(1, n)

.parallel()

.forEach(t::mult);

return t.mTotal;

}

• Ideally, a behavior’s output in a stream depends only on its input arguments

• Behaviors with side-effects can incur race conditions in parallel streams, e.g.

Sequential vs. Parallel Streams

Shared mutable state

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

Page 13: Java Streams: Sequential vs. Parallel Streamsschmidt/cs891f/2019-PDFs/X... · 7 •A parallel stream splits its elements into multiple chunk & uses the common fork-join pool to process

13

long factorial(long n) {

Total t = new Total();

LongStream

.rangeClosed(1, n)

.parallel()

.forEach(t::mult);

return t.mTotal;

}

Run in parallel

rangeClosed()

parallel()

forEach()

class Total {

public long mTotal = 1;

public void mult(long n)

{ mTotal *= n; }

}

• Ideally, a behavior’s output in a stream depends only on its input arguments

• Behaviors with side-effects can incur race conditions in parallel streams, e.g.

Sequential vs. Parallel Streams

Page 14: Java Streams: Sequential vs. Parallel Streamsschmidt/cs891f/2019-PDFs/X... · 7 •A parallel stream splits its elements into multiple chunk & uses the common fork-join pool to process

14

long factorial(long n) {

Total t = new Total();

LongStream

.rangeClosed(1, n)

.parallel()

.forEach(t::mult);

return t.mTotal;

}

class Total {

public long mTotal = 1;

public void mult(long n)

{ mTotal *= n; }

}

Beware of race conditions!!!

See en.wikipedia.org/wiki/Race_condition#Software

• Ideally, a behavior’s output in a stream depends only on its input arguments

• Behaviors with side-effects can incur race conditions in parallel streams, e.g.

Sequential vs. Parallel Streams

Page 15: Java Streams: Sequential vs. Parallel Streamsschmidt/cs891f/2019-PDFs/X... · 7 •A parallel stream splits its elements into multiple chunk & uses the common fork-join pool to process

15See jeremymanson.blogspot.com/2007/08/atomicity-visibility-and-ordering.html

long factorial(long n) {

Total t = new Total();

LongStream

.rangeClosed(1, n)

.parallel()

.forEach(t::mult);

return t.mTotal;

}

class Total {

public long mTotal = 1;

public void mult(long n)

{ mTotal *= n; }

}

Beware of inconsistent memory visibility

• Ideally, a behavior’s output in a stream depends only on its input arguments

• Behaviors with side-effects can incur race conditions in parallel streams, e.g.

Sequential vs. Parallel Streams

Page 16: Java Streams: Sequential vs. Parallel Streamsschmidt/cs891f/2019-PDFs/X... · 7 •A parallel stream splits its elements into multiple chunk & uses the common fork-join pool to process

16In Java you must avoid these hazards, i.e., the compiler & JVM won’t save you..

long factorial(long n) {

Total t = new Total();

LongStream

.rangeClosed(1, n)

.parallel()

.forEach(t::mult);

return t.mTotal;

}

class Total {

public long mTotal = 1;

public void mult(long n)

{ mTotal *= n; }

}

Only you can prevent concurrency hazards!

• Ideally, a behavior’s output in a stream depends only on its input arguments

• Behaviors with side-effects can incur race conditions in parallel streams, e.g.

Sequential vs. Parallel Streams

Page 17: Java Streams: Sequential vs. Parallel Streamsschmidt/cs891f/2019-PDFs/X... · 7 •A parallel stream splits its elements into multiple chunk & uses the common fork-join pool to process

17

End of Java Streams: Sequential vs.

Parallel Streams