introduction to java 8 parallelism...

28
Introduction to Java 8 Parallelism Frameworks Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Professor of Computer Science Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Upload: others

Post on 16-Jan-2020

24 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

Introduction to Java 8

Parallelism Frameworks

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: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

2

• Recognize how Java 8 applies functional programming features to its parallelism frameworks

Learning Objectives in this Lesson

Page 3: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

3

Learning Objectives in this Lesson• Recognize how Java 8 applies functional programming

features to its parallelism frameworks, e.g.

• Fork-join pools

Page 4: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

4

• Recognize how Java 8 applies functional programming features to its parallelism frameworks, e.g.

• Fork-join pools

• Parallel streamsfilter(not(this::urlCached))

collect(toList())

map(this::downloadImage)

flatMap(this::applyFilters)

Learning Objectives in this Lesson

Page 5: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

5

• Recognize how Java 8 applies functional programming features to its parallelism frameworks, e.g.

• Fork-join pools

• Parallel streams

• Completable futures

Learning Objectives in this Lesson

/page\ =

supplyAsync

(getStartPage())

/imgNum\ = /page\

.thenComposeAsync

(crawlHyperLinks

(page))

/imgNum\ = /page\

.thenApplyAsync

(countImages(page))

.thenApply(List::size)

/imgNum\.thenCombine(/imgNum\,

(imgNum, imgNum) ->

Integer::sum)

Completable futures also provide a reactive asynchrony programming model

Page 6: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

6

• Recognize how Java 8 applies functional programming features to its parallelism frameworks, e.g.

• Know how these features are applied in several example case study apps

New &Improved

Learning Objectives in this Lesson

Socket

Socket

List of URLs to Download

List of Filters to Apply

Persistent

Data Store

Page 7: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

7

Overview of Java 8 Parallelism Frameworks

Page 8: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

8

• Java 7 introduced the fork-join pool

See www.infoq.com/interviews/doug-lea-fork-join

Overview of Java 8 Parallelism Frameworks

Page 9: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

9

• Java 7 introduced the fork-join pool

• This framework provides high performance, fine-grained task execution for data parallelism

join joinjoin

Processsequentially

Processsequentially

Processsequentially

Processsequentially

DataSource1.1 DataSource1.2 DataSource2.1 DataSource2.2

DataSource1 DataSource2

DataSource

fork()

fork() fork()

See www.dre.vanderbilt.edu/~schmidt/PDF/DataParallelismInJava.pdf

Overview of Java 8 Parallelism Frameworks

Page 10: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

10See en.wikipedia.org/wiki/Divide_and_conquer_algorithm

Result solve(Problem problem) {

if (problem is small)

directly solve problem

else {

a. split problem into

independent parts

b. fork new sub-tasks

to solve each part

c. join all sub-tasks

d. compose result

from sub-results

}

}

• Java 7 introduced the fork-join pool

• This framework provides high performance, fine-grained task execution for data parallelism

• It supports parallel programming by solving problems via “divide & conquer”

Overview of Java 8 Parallelism Frameworks

Page 11: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

11

• Java 7 introduced the fork-join pool

• This framework provides high performance, fine-grained task execution for data parallelism

• It supports parallel programming by solving problems via “divide & conquer”

• Employs work-stealing to optimize multi-core processor performance

See gee.cs.oswego.edu/dl/papers/fj.pdf

Overview of Java 8 Parallelism Frameworks

Page 12: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

12

• Java 8 adds two new parallelism frameworks related to functional programming

Additional Frameworks & Languages

Applications

Operating System Kernel

System Libraries

Java Execution Environment (e.g., JVM)

Threading & Synchronization Packages

Java

/JN

IC+

+/C

CSee www.ibm.com/developerworks/library/j-jvmc2

Overview of Java 8 Parallelism Frameworks

Page 13: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

13

• Java 8 adds two new parallelism frameworks related to functional programming

1. Parallel streams filter(not(this::urlCached))

collect(toList())

map(this::downloadImage)

flatMap(this::applyFilters)

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

Overview of Java 8 Parallelism Frameworks

Page 14: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

14

• Java 8 adds two new parallelism frameworks related to functional programming

1. Parallel streams

• Partitions a stream into multiple substreams that run independently & combine into a “reduced” result

filter(not(this::urlCached))

collect(toList())

map(this::downloadImage)

flatMap(this::applyFilters)

Overview of Java 8 Parallelism Frameworks

Page 15: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

15

• Java 8 adds two new parallelism frameworks related to functional programming

1. Parallel streams

• Partitions a stream into multiple substreams that run independently & combine into a “reduced” result

• Chunks of data in the substreamscan be mapped to multiple threads(& cores)

filter(not(this::urlCached))

collect(toList())

map(this::downloadImage)

flatMap(this::applyFilters)

Overview of Java 8 Parallelism Frameworks

Page 16: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

16

• Java 8 adds two new parallelism frameworks related to functional programming

1. Parallel streams

• Partitions a stream into multiple substreams that run independently & combine into a “reduced” result

• Chunks of data in the substreamscan be mapped to multiple threads(& cores)

• Leverage the common fork-join pool

filter(not(this::urlCached))

collect(toList())

map(this::downloadImage)

flatMap(this::applyFilters)

Overview of Java 8 Parallelism Frameworks

See dzone.com/articles/common-fork-join-pool-and-streams

Page 17: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

17

• Java 8 adds two new parallelism frameworks related to functional programming

1. Parallel streams

• Partitions a stream into multiple substreams that run independently & combine into a “reduced” result

• Chunks of data in the substreamscan be mapped to multiple threads(& cores)

• Leverage the common fork-join pool

filter(not(this::urlCached))

collect(toList())

map(this::downloadImage)

flatMap(this::applyFilters)

Parallel streams provides fine-grained data parallelism functional programming

Overview of Java 8 Parallelism Frameworks

Page 18: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

18

• Java 8 adds two new parallelism frameworks related to functional programming

1. Parallel streams

2. Completable futures

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

/page\ =

supplyAsync

(getStartPage())

/imgNum2\ = /page\

.thenComposeAsync

(crawlHyperLinks

(page))

/imgNum1\ = /page\

.thenApplyAsync

(countImages(page))

.thenApply(List::size)

/imgNum1\.thenCombine(/imgNum2\,

(imgNum, imgNum) ->

Integer::sum)

Overview of Java 8 Parallelism Frameworks

Page 19: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

19

• Java 8 adds two new parallelism frameworks related to functional programming

1. Parallel streams

2. Completable futures

• Supports dependent actionsthat trigger upon completion of async operations

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

/page\ =

supplyAsync

(getStartPage())

/imgNum2\ = /page\

.thenComposeAsync

(crawlHyperLinks

(page))

/imgNum1\ = /page\

.thenApplyAsync

(countImages(page))

.thenApply(List::size)

/imgNum1\.thenCombine(/imgNum2\,

(imgNum, imgNum) ->

Integer::sum)

Overview of Java 8 Parallelism Frameworks

Page 20: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

20

• Java 8 adds two new parallelism frameworks related to functional programming

1. Parallel streams

2. Completable futures

• Supports dependent actionsthat trigger upon completion of async operations

• Async operations can runin parallel in thread pools

See www.nurkiewicz.com/2013/05/java-8-definitive-guide-to.html

/page\ =

supplyAsync

(getStartPage())

/imgNum2\ = /page\

.thenComposeAsync

(crawlHyperLinks

(page))

/imgNum1\ = /page\

.thenApplyAsync

(countImages(page))

.thenApply(List::size)

/imgNum1\.thenCombine(/imgNum2\,

(imgNum, imgNum) ->

Integer::sum)

Overview of Java 8 Parallelism Frameworks

Page 21: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

21

• Java 8 adds two new parallelism frameworks related to functional programming

1. Parallel streams

2. Completable futures

• Supports dependent actionsthat trigger upon completion of async operations

• Async operations can runin parallel in thread pools

Java 8 completable futures & streams can be combined to good effects!!

List of URLs to Download

filter(this::nonNull)

collect(toFuture())

map(this::downloadImageAsync)

flatMap(this::applyFiltersAsync)

map(this::checkUrlCachedAsync)

Overview of Java 8 Parallelism Frameworks

Page 22: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

22

• These frameworks often eliminate the use of synchronization or explicit threading when developing parallel apps!

Alleviates many accidental & inherent complexities of parallel programming

Overview of Java 8 Parallelism Frameworks

Page 23: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

23

• Both Java 8 frameworks use the fork-join pool framework by default

See www.oracle.com/technetwork/articles/java/fork-join-422606.html

Overview of Java 8 Parallelism Frameworks

Page 24: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

24

Summary of Example Case Study Apps

Page 25: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

25

• SearchStreamGang case study uses regular expressions to find phrases in the complete works of William Shakespeare in parallel

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

Summary of Example Case Study Apps

Starting SearchStreamGangTest

PARALLEL_SPLITERATOR executed in 409 msecs

COMPLETABLE_FUTURES_INPUTS executed in 426 msecs

COMPLETABLE_FUTURES_PHASES executed in 427 msecs

PARALLEL_STREAMS executed in 437 msecs

PARALLEL_STREAM_PHASES executed in 440 msecs

RXJAVA_PHASES executed in 485 msecs

PARALLEL_STREAM_INPUTS executed in 802 msecs

RXJAVA_INPUTS executed in 866 msecs

SEQUENTIAL_LOOPS executed in 1638 msecs

SEQUENTIAL_STREAM executed in 1958 msecs

Ending SearchStreamGangTest

Page 26: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

26

• ImageCounter recursively crawls web pages counting # of images in parallel

Summary of Example Case Study Apps

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

imgs2

imgs3

imgs1

uci.png

robot.png

...ka.png

...

wm.jpg

...

oz.jpg ...vette.

jpg

Page 27: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

27See github.com/douglascraigschmidt/LiveLessons/tree/master/ImageStreamGang

Summary of Example Case Study Apps• ImageStreamGang shows how the StreamGang framework can be combined

with Java 8 streams & completable futures to download,filter, store, & display images in parallel

collect(toList())

Parallel Streams

filter(not(this::urlCached))

map(this::downloadImage)

flatMap(this::applyFilters)

ImageStreamGang

Page 28: Introduction to Java 8 Parallelism Frameworksschmidt/cs891f/2018-PDFs/03-Java-8-concurrency-and-parallelism...•Java 8 adds two new parallelism frameworks related to functional programming

28

End of Introduction to Java 8 Parallelism Frameworks