motivating the need for java 8 completable futures (part 1) › ~schmidt › cs891f › 2018-pdfs...

30
Motivating the Need for Java 8 Completable Futures (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 07-Jul-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

Motivating the Need for

Java 8 Completable Futures (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: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

2

Learning Objectives in this Part of the Lesson• Motivate the need for Java futures

by understanding the pros & consof synchrony & asynchrony

Page 3: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

3

Motivating Need for Futures: Pros & Cons of Synchrony

Page 4: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

4

• Method calls in typical Java programs are largely synchronous

Motivating Need for Futures: Pros & Cons of Synchrony

CALLER CALLEE

searchForWord1

searchForWord2

searchForWord3

return result1

return result2

return return3

e.g., calls on Java collections & behaviors in Java 8 stream aggregate operations

Page 5: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

5

• Method calls in typical Java programs are largely synchronous

• i.e., a callee borrows the thread of its caller until its computation(s) finish

Motivating Need for Futures: Pros & Cons of Synchrony

CALLER CALLEE

searchForWord1

searchForWord2

searchForWord3

return result1

return result2

return return3

Page 6: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

6

• Method calls in typical Java programs are largely synchronous

• i.e., a callee borrows the thread of its caller until its computation(s) finish

Motivating Need for Futures: Pros & Cons of Synchrony

CALLER CALLEE

searchForWord1

searchForWord2

searchForWord3

return result1

return result2

return return3

searchForWord1

searchForWord2

searchForWord3

return result1

return result2

return return3

Page 7: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

7

• Synchronous calls have pros & cons

Motivating Need for Futures: Pros & Cons of Synchrony

Page 8: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

8

• Pros of synchronous calls:

• “Intuitive” to program & debug

Motivating Need for Futures: Pros & Cons of Synchrony

CALLER CALLEE

searchForWord1

searchForWord2

searchForWord3

return result1

return result2

return return3

Page 9: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

9

• Pros of synchronous calls:

• “Intuitive” to program & debug, e.g.

• Maps onto common two-way method patterns

Motivating Need for Futures: Pros & Cons of Synchrony

CALLER CALLEE

searchForWord1

searchForWord2

searchForWord3

return result1

return result2

return return3

See www.iro.umontreal.ca/~keller/Layla/remote.pdf

Page 10: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

10

• Pros of synchronous calls:

• “Intuitive” to program & debug, e.g.

• Maps onto common two-way method patterns

• Local caller state retained when callee returns

Motivating Need for Futures: Pros & Cons of Synchrony

CALLER CALLEE

searchForWord1

searchForWord2

searchForWord3

return result1

return result2

return return3

See wiki.c2.com/?ActivationRecord

Page 11: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

11

• Pros of synchronous calls:

• “Intuitive” to program & debug, e.g.

• Maps onto common two-way method patterns

• Local caller state retained when callee returns

Motivating Need for Futures: Pros & Cons of Synchrony

CALLER CALLEE

searchForWord1

searchForWord2

searchForWord3

return result1

return result2

return return3

See wiki.c2.com/?ActivationRecord

byte[] downloadContent(URL url) {

byte[] buf = new byte[BUFSIZ];

ByteArrayOutputStream os =

new ByteArrayOutputStream();

InputStream is = url.openStream();

for (int bytes;

(bytes = is.read(buf)) > 0;)

os.write(buf, 0, bytes); ...

Page 12: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

12

• Cons of synchronous calls:

• May not leverage all parallelism available in multi-core systems

Motivating Need for Futures: Pros & Cons of Synchrony

CALLER CALLEE

searchForWord1

searchForWord2

searchForWord3

return result1

return result2

return return3

See www.ibm.com/developerworks/library/j-jvmc3

Page 13: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

13

• Cons of synchronous calls:

• May not leverage all parallelism available in multi-core systems

• Blocking threads incur overhead

• e.g., synchronization, context switching, data movement, & memory management costs

Motivating Need for Futures: Pros & Cons of Synchrony

See www.ibm.com/developerworks/library/j-jvmc3

Page 14: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

14

Motivating Need for Futures: Pros & Cons of Synchrony

EfficientResourceUtilization

EfficientPerformance

• Cons of synchronous calls:

• May not leverage all parallelism available in multi-core systems

• Blocking threads incur overhead

• Selecting right # of threads is hard

List<Image> filteredImages = urls

.parallelStream()

.filter(not(this::urlCached))

.map(this::downloadImage)

.flatMap(this::applyFilters)

.collect(toList());Image downloadImage(URL url){

return new Image(url,

downloadContent(url));

}

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

Page 15: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

15

Motivating Need for Futures: Pros & Cons of Synchrony• Cons of synchronous calls:

• May not leverage all parallelism available in multi-core systems

• Blocking threads incur overhead

• Selecting right # of threads is hard

EfficientResourceUtilization

EfficientPerformance

List<Image> filteredImages = urls

.parallelStream()

.filter(not(this::urlCached))

.map(this::downloadImage)

.flatMap(this::applyFilters)

.collect(toList()); A large # of threads may improve performance at the

cost of wasted resources

Page 16: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

16

• Cons of synchronous calls:

• May not leverage all parallelism available in multi-core systems

• Blocking threads incur overhead

• Selecting right # of threads is hard

Motivating Need for Futures: Pros & Cons of Synchrony

EfficientResourceUtilization

EfficientPerformance

List<Image> filteredImages = urls

.parallelStream()

.filter(not(this::urlCached))

.map(this::downloadImage)

.flatMap(this::applyFilters)

.collect(toList()); A small # of threads may conserve resources at the

cost of performance

Page 17: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

17

Motivating Need for Futures: Pros & Cons of Synchrony• Cons of synchronous calls:

• May not leverage all parallelism available in multi-core systems

• Blocking threads incur overhead

• Selecting right # of threads is hard

EfficientResourceUtilization

EfficientPerformance

Particularly tricky for I/O-bound programs that need

more threads to run efficiently

Page 18: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

18

• Cons of synchronous calls:

• May not leverage all parallelism available in multi-core systems

• May need to change common fork-join pool size in a Java 8 parallel stream

Motivating Need for Futures: Pros & Cons of Synchrony

See dzone.com/articles/think-twice-using-java-8

Page 19: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

19

Motivating Need for Futures: Pros & Cons of Asynchrony

Page 20: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

20

• Asynchronous (async) calls can alleviate the limitations with synchronous calls

Motivating Need for Futures: Pros & Cons of Asynchrony

See en.wikipedia.org/wiki/Asynchrony_(computer_programming)

Page 21: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

21

• Asynchronous (async) calls can alleviate the limitations with synchronous calls

• Asynchrony is a means of concurrent programming where a unit of work has certain properties

Motivating Need for Futures: Pros & Cons of Asynchrony

Page 22: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

22

• Asynchronous (async) calls can alleviate the limitations with synchronous calls

• Asynchrony is a means of concurrent programming where a unit of work has certain properties

• Runs separately from the calling thread “in the background”

Motivating Need for Futures: Pros & Cons of Asynchrony

Background thread

Page 23: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

23

• Asynchronous (async) calls can alleviate the limitations with synchronous calls

• Asynchrony is a means of concurrent programming where a unit of work has certain properties

• Runs separately from the calling thread “in the background”

• Notifies the calling thread of itscompletion, failure, or progress

Motivating Need for Futures: Pros & Cons of Asynchrony

Calling thread

Page 24: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

24

• Asynchronous calls have pros & cons

Motivating Need for Futures: Pros & Cons of Asynchrony

Page 25: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

25

• Pros of asynchronous calls

• Responsiveness

• A thread does not block waiting for other requests to complete

Motivating Need for Futures: Pros & Cons of Asynchrony

Page 26: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

26

• Pros of asynchronous calls

• Responsiveness

• Elasticity

• Multiple requests can run scalably& concurrently in multiple cores

Motivating Need for Futures: Pros & Cons of Asynchrony

Page 27: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

27

• Cons of asynchronous calls

• Unpredictability

• Response times may not be predictable

Motivating Need for Futures: Pros & Cons of Asynchrony

Page 28: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

28

• Cons of asynchronous calls

• Unpredictability

• Response times may not be predictable

• Results can occur in a different order than the original calls were made

Motivating Need for Futures: Pros & Cons of Asynchrony

CALLER CALLEE

searchForWord1

future result1

searchForWord2

future result3

searchForWord3

future result2

future1

future2

future3

future result1

future result3

future result2

Page 29: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

29

• Cons of asynchronous calls

• Unpredictability

• Complicated debugging

• Errors can be hard to trackdue to unpredictability

Motivating Need for Futures: Pros & Cons of Asynchrony

See www.jetbrains.com/help/idea/tutorial-java-debugging-deep-dive.html

Page 30: Motivating the Need for Java 8 Completable Futures (Part 1) › ~schmidt › cs891f › 2018-PDFs › 02... · 2018-10-29 · 4 •Method calls in typical Java programs are largely

30

End of Motivating the Need for Java 8 Completable

Futures (Part 1)