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

Post on 08-Aug-2020

5 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Java 8 CompletableFutures

ImageStreamGang Example (Part 1)

Douglas C. Schmidtd.schmidt@vanderbilt.edu

www.dre.vanderbilt.edu/~schmidt

Professor of Computer Science

Institute for Software

Integrated Systems

Vanderbilt University

Nashville, Tennessee, USA

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

3

Overview of the CompletableFutures ImageStreamGang

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

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

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

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

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)

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)

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)

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)

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)

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)

14

End of Java 8 CompletableFutures ImageStreamGang

Example (Part 1)

top related