java 8 completablefutures imagestreamgang example (part 1)schmidt/cs891f/2018-pdfs/... · 2...

14
Java 8 CompletableFutures ImageStreamGang Example (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

Upload: others

Post on 08-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java 8 CompletableFutures ImageStreamGang Example (Part 1)schmidt/cs891f/2018-PDFs/... · 2 Learning Objectives in this Part of the Lesson •Understand the design of the Java 8 completable

Java 8 CompletableFutures

ImageStreamGang Example (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: Java 8 CompletableFutures ImageStreamGang Example (Part 1)schmidt/cs891f/2018-PDFs/... · 2 Learning Objectives in this Part of the Lesson •Understand the design of the Java 8 completable

2

Learning Objectives in this Part of the Lesson• Understand the design of the Java 8 completable future

version 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: Java 8 CompletableFutures ImageStreamGang Example (Part 1)schmidt/cs891f/2018-PDFs/... · 2 Learning Objectives in this Part of the Lesson •Understand the design of the Java 8 completable

3

Overview of the CompletableFutures ImageStreamGang

Page 4: Java 8 CompletableFutures ImageStreamGang Example (Part 1)schmidt/cs891f/2018-PDFs/... · 2 Learning Objectives in this Part of the Lesson •Understand the design of the Java 8 completable

4

• ImageStreamGang applies completable future to optimize performance

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

Overview of Completable Futures ImageStreamGang

Socket

Socket

Persistent

Data Store

List of Filters to Apply

List of URLs to Download

Page 5: Java 8 CompletableFutures ImageStreamGang Example (Part 1)schmidt/cs891f/2018-PDFs/... · 2 Learning Objectives in this Part of the Lesson •Understand the design of the Java 8 completable

5

• ImageStreamGang applies completable future to optimize performance

• Ignore cached images

• Download non-cached images

• Apply list of filters to each image

• Store filtered images in the file system

• Display images to the user

Overview of Completable Futures ImageStreamGang

Socket

Socket

Persistent

Data Store

List of Filters to Apply

List of URLs to Download

Page 6: Java 8 CompletableFutures ImageStreamGang Example (Part 1)schmidt/cs891f/2018-PDFs/... · 2 Learning Objectives in this Part of the Lesson •Understand the design of the Java 8 completable

6

• The behaviors in this pipeline differ from the earlier parallel streams variant

Overview of Completable Futures ImageStreamGang

map(this::downloadImageAsync)

thenAccept(this::log)

flatMap(this::applyFiltersAsync)

collect(toFuture())

map(this::checkUrlCachedAsync)

List of URLs to Download

collect(toList())

filter(not(this::urlCached))

map(this::downloadImage)

flatMap(this::applyFilters)

Parallel Streams Completable Futures

Page 7: Java 8 CompletableFutures ImageStreamGang Example (Part 1)schmidt/cs891f/2018-PDFs/... · 2 Learning Objectives in this Part of the Lesson •Understand the design of the Java 8 completable

7

• The behaviors in this pipeline differ from the earlier parallel streams variant, e.g.

• Ignore cached images asynchronously

Overview of Completable Futures ImageStreamGang

map(this::downloadImageAsync)

thenAccept(this::log)

flatMap(this::applyFiltersAsync)

collect(toFuture())

map(this::checkUrlCachedAsync)

List of URLs to Download

Page 8: Java 8 CompletableFutures ImageStreamGang Example (Part 1)schmidt/cs891f/2018-PDFs/... · 2 Learning Objectives in this Part of the Lesson •Understand the design of the Java 8 completable

8

• The behaviors in this pipeline differ from the earlier parallel streams variant, e.g.

• Ignore cached images asynchronously

• Download non-cached imagesasynchronously

Overview of Completable Futures ImageStreamGangList of URLs to Download

map(this::downloadImageAsync)

thenAccept(this::log)

flatMap(this::applyFiltersAsync)

collect(toFuture())

map(this::checkUrlCachedAsync)

Page 9: Java 8 CompletableFutures ImageStreamGang Example (Part 1)schmidt/cs891f/2018-PDFs/... · 2 Learning Objectives in this Part of the Lesson •Understand the design of the Java 8 completable

9

List of URLs to Download

…• The behaviors in this pipeline differ from the

earlier parallel streams variant, e.g.

• Ignore cached images asynchronously

• Download non-cached imagesasynchronously

• As downloads complete asynchronously apply a list of filters & store filtered images in file system

Overview of Completable Futures ImageStreamGang

map(this::downloadImageAsync)

thenAccept(this::log)

flatMap(this::applyFiltersAsync)

collect(toFuture())

map(this::checkUrlCachedAsync)

Page 10: Java 8 CompletableFutures ImageStreamGang Example (Part 1)schmidt/cs891f/2018-PDFs/... · 2 Learning Objectives in this Part of the Lesson •Understand the design of the Java 8 completable

10

List of URLs to Download

…• The behaviors in this pipeline differ from the

earlier parallel streams variant, e.g.

• Ignore cached images asynchronously

• Download non-cached imagesasynchronously

• As downloads complete asynchronously apply a list of filters & store filtered images in file system

• Trigger all the stream processing torun asynchronously

Overview of Completable Futures ImageStreamGang

map(this::downloadImageAsync)

thenAccept(this::log)

flatMap(this::applyFiltersAsync)

collect(toFuture())

map(this::checkUrlCachedAsync)

Page 11: Java 8 CompletableFutures ImageStreamGang Example (Part 1)schmidt/cs891f/2018-PDFs/... · 2 Learning Objectives in this Part of the Lesson •Understand the design of the Java 8 completable

11

List of URLs to Download

…• The behaviors in this pipeline differ from the

earlier parallel streams variant, e.g.

• Ignore cached images asynchronously

• Download non-cached imagesasynchronously

• As downloads complete asynchronously apply a list of filters & store filtered images in file system

• Trigger all the stream processing torun asynchronously

• Get results of asynchronous computations

Overview of Completable Futures ImageStreamGang

map(this::downloadImageAsync)

thenAccept(this::log)

flatMap(this::applyFiltersAsync)

collect(toFuture())

map(this::checkUrlCachedAsync)

Page 12: Java 8 CompletableFutures ImageStreamGang Example (Part 1)schmidt/cs891f/2018-PDFs/... · 2 Learning Objectives in this Part of the Lesson •Understand the design of the Java 8 completable

12

• The behaviors in this pipeline differ from the earlier parallel streams variant, e.g.

• Ignore cached images asynchronously

• Download non-cached imagesasynchronously

• As downloads complete asynchronously apply a list of filters & store filtered images in file system

• Trigger all the stream processing torun asynchronously

• Get results of asynchronous computations

• Ultimately display images to user

Overview of Completable Futures ImageStreamGangList of URLs to Download

map(this::downloadImageAsync)

thenAccept(this::log)

flatMap(this::applyFiltersAsync)

collect(toFuture())

map(this::checkUrlCachedAsync)

Page 13: Java 8 CompletableFutures ImageStreamGang Example (Part 1)schmidt/cs891f/2018-PDFs/... · 2 Learning Objectives in this Part of the Lesson •Understand the design of the Java 8 completable

13

List of URLs to Download

…• Combining completable futures & streams

helps to close the gap between the design intent & the implementation

Overview of Completable Futures ImageStreamGang

map(this::downloadImageAsync)

thenAccept(this::log)

flatMap(this::applyFiltersAsync)

collect(toFuture())

map(this::checkUrlCachedAsync)

Page 14: Java 8 CompletableFutures ImageStreamGang Example (Part 1)schmidt/cs891f/2018-PDFs/... · 2 Learning Objectives in this Part of the Lesson •Understand the design of the Java 8 completable

14

End of Java 8 CompletableFutures ImageStreamGang

Example (Part 1)