Overview of the Java Parallel ImageStreamGang Case Study

35
Overview of the Java Parallel ImageStreamGang Case Study 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 Overview of the Java Parallel ImageStreamGang Case Study

Page 1: Overview of the Java Parallel ImageStreamGang Case Study

Overview of the Java Parallel

ImageStreamGang Case Study

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 the Java Parallel ImageStreamGang Case Study

2

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

SocketSocket

List of URLs to Download

List of Filters to Apply

Persistent

Data Store

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

Page 3: Overview of the Java Parallel ImageStreamGang Case Study

3

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

• It applies several Java parallelism frameworks

collect(toList())

Parallel Streams

filter(not(this::urlCached))

map(this::downloadImage)

flatMap(this::applyFilters)

map(this::downloadImageAsync)

thenAccept(this::log)

flatMap(this::applyFiltersAsync)

collect(toFuture())

map(this::checkUrlCachedAsync)

Completable Futures

Page 4: Overview of the Java Parallel ImageStreamGang Case Study

4

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

• It applies several Java parallelism frameworks

collect(toList())

Parallel Streams

filter(not(this::urlCached))

map(this::downloadImage)

flatMap(this::applyFilters)

map(this::downloadImageAsync)

thenAccept(this::log)

flatMap(this::applyFiltersAsync)

collect(toFuture())

map(this::checkUrlCachedAsync)

Completable Futures

Parallel streams must use fork-join pool framework

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

Page 5: Overview of the Java Parallel ImageStreamGang Case Study

5

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

• It applies several Java parallelism frameworks

collect(toList())

Parallel Streams

filter(not(this::urlCached))

map(this::downloadImage)

flatMap(this::applyFilters)

map(this::downloadImageAsync)

thenAccept(this::log)

flatMap(this::applyFiltersAsync)

collect(toFuture())

map(this::checkUrlCachedAsync)

Completable Futures

Completable futures may use fork-join pool framework

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

Page 6: Overview of the Java Parallel ImageStreamGang Case Study

6

Overview of the Pattern–Oriented ImageStream

Gang App

Page 7: Overview of the Java Parallel ImageStreamGang Case Study

7

Overview of the Pattern-Oriented ImageStreamGang App

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

ImageStreamGang

• This app combines streams & completable futures with the StreamGangframework to download, transform, store, & display images

collect(toList())

Parallel Streams…

filter(not(this::urlCached))

map(this::downloadImage)

flatMap(this::applyFilters)

map(this::downloadImageAsync)

thenAccept(this::log)

flatMap(this::applyFiltersAsync)

collect(toFuture())

map(this::checkUrlCachedAsync)

Completable Futures

Page 8: Overview of the Java Parallel ImageStreamGang Case Study

8

ImageStreamGang

• This app combines streams & completable futures with the StreamGangframework to download, transform, store, & display images, e.g.

Overview of the Pattern-Oriented ImageStreamGang App

Prompt user for list of URLs to download

SocketSocket

Page 9: Overview of the Java Parallel ImageStreamGang Case Study

9User supplies the list of URLs to download

List of URLs to Download

Overview of the Pattern-Oriented ImageStreamGang App

ImageStreamGang

• This app combines streams & completable futures with the StreamGangframework to download, transform, store, & display images, e.g.

SocketSocket

Page 10: Overview of the Java Parallel ImageStreamGang Case Study

10Download images via one or more threads

List of URLs to Download

Overview of the Pattern-Oriented ImageStreamGang App

SocketSocket

• This app combines streams & completable futures with the StreamGangframework to download, transform, store, & display images, e.g.

Page 11: Overview of the Java Parallel ImageStreamGang Case Study

11

List of Filters to Apply

Apply filters to transform downloaded images

List of URLs to Download

Overview of the Pattern-Oriented ImageStreamGang App

SocketSocket

• This app combines streams & completable futures with the StreamGangframework to download, transform, store, & display images, e.g.

Page 12: Overview of the Java Parallel ImageStreamGang Case Study

12

List of Filters to Apply

Output filtered images to persistent storage

List of URLs to Download

Overview of the Pattern-Oriented ImageStreamGang App• This app combines streams & completable futures with the StreamGang

framework to download, transform, store, & display images, e.g.

SocketSocket

Persistent

Data Store

Page 13: Overview of the Java Parallel ImageStreamGang Case Study

13

• The ImageStreamGang app applies a range of modern Java features

Overview of the Pattern-Oriented ImageStreamGang App

See www.manning.com/books/modern-java-in-action

Page 14: Overview of the Java Parallel ImageStreamGang Case Study

14

• The ImageStreamGang app applies a range of modern Java features, e.g.

• Sequential & parallel streams

List<Image> filteredImages =

getInput()

.parallelStream()

.filter(not(this::urlCached))

.map(this::downloadImage)

.flatMap(this::applyFilters)

.collect(toList());

Overview of the Pattern-Oriented ImageStreamGang App

We’ll cover parallel streams now

Page 15: Overview of the Java Parallel ImageStreamGang Case Study

15

• The ImageStreamGang app applies a range of modern Java features, e.g.

• Sequential & parallel streams

• Completable futures

getInput()

.stream()

.map(this::checkUrlCachedAsync)

.map(this::downloadImageAsync)

.flatMap(this::applyFiltersAsync)

.collect(toFuture())

.thenAccept

(stream ->

log(stream.flatMap(Optional::stream),

urls.size()))

.join();

Overview of the Pattern-Oriented ImageStreamGang App

We cover completable futures later

Page 16: Overview of the Java Parallel ImageStreamGang Case Study

16

• The ImageStreamGang app applies a range of modern Java features, e.g.

• Sequential & parallel streams

• Completable futures

• Lambda expressions & method referencesRunnable mCompletionHook =

() -> MainActivity.this.runOnUiThread

(this::goToResultActivity);

Overview of the Pattern-Oriented ImageStreamGang App

We covered these foundational Java features earlier

Page 17: Overview of the Java Parallel ImageStreamGang Case Study

17

• The ImageStreamGang app applies a range of modern Java features, e.g.

• Sequential & parallel streams

• Completable futures

• Lambda expressions & method references

Runnable mCompletionHook =

() -> MainActivity.this.runOnUiThread

(this::goToResultActivity);

versus

Runnable mCompletionHook = new Runnable() {

public void run() {

MainActivity.this.runOnUiThread

(new Runnable() { public void run()

{ goToResultActivity(); } }); }};

Overview of the Pattern-Oriented ImageStreamGang App

Page 18: Overview of the Java Parallel ImageStreamGang Case Study

18

Overview of Patterns Applied in the

ImageStreamGang App

Page 19: Overview of the Java Parallel ImageStreamGang Case Study

19

• “Gang-of-Four” & POSA patterns are applied to enhance its framework-based architecture

See en.wikipedia.org/wiki/Design_Patterns & www.dre.vanderbilt.edu/~schmidt/POSA

Overview of Patterns Applied in the ImageStreamGang App

Page 20: Overview of the Java Parallel ImageStreamGang Case Study

20

• Some patterns are essential to its design

Overview of Patterns Applied in the ImageStreamGang App

Page 21: Overview of the Java Parallel ImageStreamGang Case Study

21

• Some patterns are essential to its design

• Pipes and Filters

• Divide application’s tasks into several self-contained data processing steps & connect these steps via intermediate data buffers to a data processing pipeline

See www.hillside.net/plop/2011/papers/B-10-Hanmer.pdf

Overview of Patterns Applied in the ImageStreamGang App

Page 22: Overview of the Java Parallel ImageStreamGang Case Study

22

• Some patterns are essential to its design

• Future

• Provides a proxy to a client when it invokes a service to keep track of the state of the service’sconcurrent computation & only returns a value to the client when the computation completes

See en.wikipedia.org/wiki/Futures_and_promises

Overview of Patterns Applied in the ImageStreamGang App

Page 23: Overview of the Java Parallel ImageStreamGang Case Study

23

Resource Pool

• Some patterns are essential to its design

• Resource Pool

• Prevents expensive acquisition & release of resources by recycling resources no longer needed

See kircher-schwanninger.de/michael/publications/Pooling.pdf

Overview of Patterns Applied in the ImageStreamGang App

Page 24: Overview of the Java Parallel ImageStreamGang Case Study

24

• Some patterns are essential to its design

• Template Method

• Defines the overall structure of a method, while allowing subclasses to refine, or redefine, certain steps

See en.wikipedia.org/wiki/Template_method_pattern

Overview of Patterns Applied in the ImageStreamGang App

Page 25: Overview of the Java Parallel ImageStreamGang Case Study

25

• Some patterns are essential to its design

• Factory Method

• Encapsulate the concrete details of object creation inside a factory method, rather than letting clients create the object themselves

See en.wikipedia.org/wiki/Factory_method_pattern

Overview of Patterns Applied in the ImageStreamGang App

Page 26: Overview of the Java Parallel ImageStreamGang Case Study

26

• Some patterns are essential to its design

• Decorator

• Allows behavior to be added to an individual object, dynamically, without affecting the behavior of other objects from the same class

See en.wikipedia.org/wiki/Decorator_pattern

Overview of Patterns Applied in the ImageStreamGang App

Page 27: Overview of the Java Parallel ImageStreamGang Case Study

27

• Other patterns are also applied

• Singleton

• Ensure a class has only one instance & provide a global point of access to it

See en.wikipedia.org/wiki/Singleton_pattern

Overview of Patterns Applied in the ImageStreamGang App

Page 28: Overview of the Java Parallel ImageStreamGang Case Study

28

• Other patterns are also applied

• Command Processor

• Packages a piece of application functionality—as well as its parameterization in an object—to make it usable in another context, such as later in time or in a different thread

See www.dre.vanderbilt.edu/~schmidt/CommandProcessor.pdf

Overview of Patterns Applied in the ImageStreamGang App

Page 29: Overview of the Java Parallel ImageStreamGang Case Study

29

Strategy for Understanding the ImageStreamGang App

Page 30: Overview of the Java Parallel ImageStreamGang Case Study

30

• This app is complicated & contains many classes

Strategy for Understanding the ImageStreamGang App

Page 31: Overview of the Java Parallel ImageStreamGang Case Study

31

• This app is complicated & contains many classes

• We therefore analyze it from various perspectives

Strategy for Understanding the ImageStreamGang App

Including pattern-oriented design, data flows, & detailed code walkthroughs

Page 32: Overview of the Java Parallel ImageStreamGang Case Study

32

• This app is complicated & contains many classes

• We therefore analyze it from various perspectives

• Watch this entire lesson carefully to understand how it all works

Strategy for Understanding the ImageStreamGang App

Page 33: Overview of the Java Parallel ImageStreamGang Case Study

33

• This app is complicated & contains many classes

• We therefore analyze it from various perspectives

• Watch this entire lesson carefully to understand how it all works

• Visualize the data flow in a parallel stream

Strategy for Understanding the ImageStreamGang App

Page 34: Overview of the Java Parallel ImageStreamGang Case Study

34

• This app is complicated & contains many classes

• We therefore analyze it from various perspectives

• Watch this entire lesson carefully to understand how it all works

• Visualize the data flow in a parallel stream

• Run/read the code to see how it all works

Strategy for Understanding the ImageStreamGang App

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

Page 35: Overview of the Java Parallel ImageStreamGang Case Study

35

End of Overview of the Java Parallel ImageStreamGang

Case Study