![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](https://reader034.vdocuments.us/reader034/viewer/2022050516/5f9fdd64618b8909ae05c83d/html5/thumbnails/1.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022050516/5f9fdd64618b8909ae05c83d/html5/thumbnails/2.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022050516/5f9fdd64618b8909ae05c83d/html5/thumbnails/3.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022050516/5f9fdd64618b8909ae05c83d/html5/thumbnails/4.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022050516/5f9fdd64618b8909ae05c83d/html5/thumbnails/5.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022050516/5f9fdd64618b8909ae05c83d/html5/thumbnails/6.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022050516/5f9fdd64618b8909ae05c83d/html5/thumbnails/7.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022050516/5f9fdd64618b8909ae05c83d/html5/thumbnails/8.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022050516/5f9fdd64618b8909ae05c83d/html5/thumbnails/9.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022050516/5f9fdd64618b8909ae05c83d/html5/thumbnails/10.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022050516/5f9fdd64618b8909ae05c83d/html5/thumbnails/11.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022050516/5f9fdd64618b8909ae05c83d/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022050516/5f9fdd64618b8909ae05c83d/html5/thumbnails/13.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022050516/5f9fdd64618b8909ae05c83d/html5/thumbnails/14.jpg)
14
End of Java 8 CompletableFutures ImageStreamGang
Example (Part 1)