motivating the need for java 8 completable futures …schmidt/cs891f/2018-pdfs/04...6 •pros of...

19
Motivating the Need for Java 8 Completable Futures (Part 3) 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 14-Aug-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Motivating the Need for Java 8 Completable Futures …schmidt/cs891f/2018-PDFs/04...6 •Pros of async calls with Java futures •May leverage parallelism more effectively with fewer

Motivating the Need for

Java 8 Completable Futures (Part 3)

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 …schmidt/cs891f/2018-PDFs/04...6 •Pros of async calls with Java futures •May leverage parallelism more effectively with fewer

2

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

by understanding the pros & consof synchrony & asynchrony

• Know how Java futures provide the foundation for completablefutures in Java 8

• Motivate the need for Java 8 completable futures by recognizingthe pros & cons with Java futures

Page 3: Motivating the Need for Java 8 Completable Futures …schmidt/cs891f/2018-PDFs/04...6 •Pros of async calls with Java futures •May leverage parallelism more effectively with fewer

3

Motivating the Need for Completable Futures

Page 4: Motivating the Need for Java 8 Completable Futures …schmidt/cs891f/2018-PDFs/04...6 •Pros of async calls with Java futures •May leverage parallelism more effectively with fewer

4

• Pros & cons of async calls with Java futures

Motivating the Need for Completable Futures

Page 5: Motivating the Need for Java 8 Completable Futures …schmidt/cs891f/2018-PDFs/04...6 •Pros of async calls with Java futures •May leverage parallelism more effectively with fewer

5

• Pros of async calls with Java futures

• May leverage parallelism more effectively with fewer threads

Motivating the Need for Completable Futures

45,000+ phrases"do", "re", "mi", "fa",

"so", "la", "ti", "do"

Search Words

Input Strings to Search

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

submit()

3.take()

4.run()

runnable

SearchResults

SearchResults

SearchResults

SearchResults

Completion

Queue

runnable

WorkQueue

2.offer()

ExecutorCompletionService

5.add()

Variable

WorkerThreadstake()

1.submit(task)

7.take()

take()

Page 6: Motivating the Need for Java 8 Completable Futures …schmidt/cs891f/2018-PDFs/04...6 •Pros of async calls with Java futures •May leverage parallelism more effectively with fewer

6

• Pros of async calls with Java futures

• May leverage parallelism more effectively with fewer threads, e.g.,

• Queue async computations for execution in a pool of threads

Motivating the Need for Completable FuturesmCompletionService

.submit(() ->

searchForWord(word,

input));

submit()

3.take()

4.run()

runnable

SearchResults

SearchResults

SearchResults

SearchResults

Completion

Queue

runnable

WorkQueue

2.offer()

ExecutorCompletionService

5.add()

Variable

WorkerThreadstake()

1.submit(task)

7.take()

take()

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorCompletionService.html#submit

Page 7: Motivating the Need for Java 8 Completable Futures …schmidt/cs891f/2018-PDFs/04...6 •Pros of async calls with Java futures •May leverage parallelism more effectively with fewer

7

• Pros of async calls with Java futures

• May leverage parallelism more effectively with fewer threads, e.g.,

• Queue async computations for execution in a pool of threads

• Automatically tune # of threads

Motivating the Need for Completable Futures

submit()

3.take()

4.run()

runnable

SearchResults

SearchResults

SearchResults

SearchResults

Completion

Queue

runnable

WorkQueue

2.offer()

ExecutorCompletionService

5.add()

Variable

WorkerThreadstake()

1.submit(task)

7.take()

take()

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html#newCachedThreadPool

mCompletionService

.submit(() ->

searchForWord(word,

input));

Page 8: Motivating the Need for Java 8 Completable Futures …schmidt/cs891f/2018-PDFs/04...6 •Pros of async calls with Java futures •May leverage parallelism more effectively with fewer

8

• Pros of async calls with Java futures

• May leverage parallelism more effectively with fewer threads, e.g.,

• Queue async computations for execution in a pool of threads

• Automatically tune # of threads

• Results can be taken from queue of completed futures

Motivating the Need for Completable Futures

submit()

3.take()

4.run()

runnable

SearchResults

SearchResults

SearchResults

SearchResults

Completion

Queue

runnable

WorkQueue

2.offer()

ExecutorCompletionService

5.add()

Variable

WorkerThreadstake()

1.submit(task)

7.take()

take()

Future<SearchResults> resultF =

mCompletionService.take();

resultF.get().print()

take() blocks, but get() doesn’t

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorCompletionService.html#take

Page 9: Motivating the Need for Java 8 Completable Futures …schmidt/cs891f/2018-PDFs/04...6 •Pros of async calls with Java futures •May leverage parallelism more effectively with fewer

9

• Pros of async calls with Java futures

• May leverage parallelism more effectively with fewer threads

• Can block until the result of an async two-way task is available

Motivating the Need for Completable FuturesString f1 = "62675744/15668936";

String f2 = "609136/913704";

Future<BigFraction> f =

commonPool().submit(() -> {

BigFraction bf1 =

new BigFraction(f1);

BigFraction bf2 =

new BigFraction(f2);

return bf1.multiply(bf2);

});

...

BigFraction result =

f.get();

Page 10: Motivating the Need for Java 8 Completable Futures …schmidt/cs891f/2018-PDFs/04...6 •Pros of async calls with Java futures •May leverage parallelism more effectively with fewer

10

• Pros of async calls with Java futures

• May leverage parallelism more effectively with fewer threads

• Can block until the result of an async two-way task is available

• Can also poll or time-block

Motivating the Need for Completable FuturesString f1 = "62675744/15668936";

String f2 = "609136/913704";

Future<BigFraction> f =

commonPool().submit(() -> {

BigFraction bf1 =

new BigFraction(f1);

BigFraction bf2 =

new BigFraction(f2);

return bf1.multiply(bf2);

});

...

BigFraction result =

f.get(n, MILLISECONDS);

Page 11: Motivating the Need for Java 8 Completable Futures …schmidt/cs891f/2018-PDFs/04...6 •Pros of async calls with Java futures •May leverage parallelism more effectively with fewer

11

• Pros of async calls with Java futures

• May leverage parallelism more effectively with fewer threads

• Can block until the result of an async two-way task is available

• Can be canceled & tested to see if a task is done

Motivating the Need for Completable FuturesString f1 = "62675744/15668936";

String f2 = "609136/913704";

Future<BigFraction> f =

commonPool().submit(() -> {

BigFraction bf1 =

new BigFraction(f1);

BigFraction bf2 =

new BigFraction(f2);

return bf1.multiply(bf2);

});

...

if (!(f.isDone()

|| !f.isCancelled()))

f.cancel();

Page 12: Motivating the Need for Java 8 Completable Futures …schmidt/cs891f/2018-PDFs/04...6 •Pros of async calls with Java futures •May leverage parallelism more effectively with fewer

12

Motivating the Need for Completable Futures• Cons of async calls with Java futures

• Limited feature set

Page 13: Motivating the Need for Java 8 Completable Futures …schmidt/cs891f/2018-PDFs/04...6 •Pros of async calls with Java futures •May leverage parallelism more effectively with fewer

13

• Cons of async calls with Java futures

• Limited feature set

• Cannot be completed explicitly

• e.g., additional mechanismslike FutureTask are needed

Motivating the Need for Completable Futures

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/FutureTask.html

Page 14: Motivating the Need for Java 8 Completable Futures …schmidt/cs891f/2018-PDFs/04...6 •Pros of async calls with Java futures •May leverage parallelism more effectively with fewer

14

• Cons of async calls with Java futures

• Limited feature set

• Cannot be completed explicitly

• Cannot be chained fluently to handle async results

Motivating the Need for Completable Futures

See en.wikipedia.org/wiki/Fluent_interface

Page 15: Motivating the Need for Java 8 Completable Futures …schmidt/cs891f/2018-PDFs/04...6 •Pros of async calls with Java futures •May leverage parallelism more effectively with fewer

15

• Cons of async calls with Java futures

• Limited feature set

• Cannot be completed explicitly

• Cannot be chained fluently to handle async results

• Cannot be triggered reactively

• i.e., must (timed-)wait or poll

Motivating the Need for Completable FuturesString f1 = "62675744/15668936";

String f2 = "609136/913704";

Future<BigFraction> f =

commonPool().submit(() -> {

BigFraction bf1 =

new BigFraction(f1);

BigFraction bf2 =

new BigFraction(f2);

return bf1.multiply(bf2);

});

...

BigFraction result = f.get();

// f.get(10, MILLISECONDS);

// f.get(0, 0);

Page 16: Motivating the Need for Java 8 Completable Futures …schmidt/cs891f/2018-PDFs/04...6 •Pros of async calls with Java futures •May leverage parallelism more effectively with fewer

16

• Cons of async calls with Java futures

• Limited feature set

• Cannot be completed explicitly

• Cannot be chained fluently to handle async results

• Cannot be triggered reactively

• i.e., must (timed-)wait or poll

Motivating the Need for Completable FuturesString f1 = "62675744/15668936";

String f2 = "609136/913704";

Future<BigFraction> f =

commonPool().submit(() -> {

BigFraction bf1 =

new BigFraction(f1);

BigFraction bf2 =

new BigFraction(f2);

return bf1.multiply(bf2);

});

...

BigFraction result = f.get();

// f.get(10, MILLISECONDS);

// f.get(0, 0);

Nearly always the wrong

thing to do!!

See crondev.blog/2017/01/23/timeouts-with-java-8-completablefuture-youre-probably-doing-it-wrong

Page 17: Motivating the Need for Java 8 Completable Futures …schmidt/cs891f/2018-PDFs/04...6 •Pros of async calls with Java futures •May leverage parallelism more effectively with fewer

17

• Cons of async calls with Java futures

• Limited feature set

• Cannot be completed explicitly

• Cannot be chained fluently to handle async results

• Cannot be triggered reactively

• Cannot be treated efficientlyas a collection of futures

Motivating the Need for Completable FuturesFuture<BigFraction> future1 =

commonPool().submit(() -> {

... });

Future<BigFraction> future2 =

commonPool().submit(() -> {

... });

...

future1.get();

future2.get();

Can’t wait efficiently for the completion of whichever async computation finishes first

Page 18: Motivating the Need for Java 8 Completable Futures …schmidt/cs891f/2018-PDFs/04...6 •Pros of async calls with Java futures •May leverage parallelism more effectively with fewer

18

• Cons of async calls with Java futures

• Limited feature set

• Cannot be completed explicitly

• Cannot be chained fluently to handle async results

• Cannot be triggered reactively

• Cannot be treated efficientlyas a collection of futures

Motivating the Need for Completable Futures

In general, it’s awkward & inefficient to “compose” multiple futures

Page 19: Motivating the Need for Java 8 Completable Futures …schmidt/cs891f/2018-PDFs/04...6 •Pros of async calls with Java futures •May leverage parallelism more effectively with fewer

19

End of Motivating the Need for Java 8 Completable

Futures (Part 3)