The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of...

25
The Pros & Cons of Asynchrony Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Professor of Computer Science Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Transcript of The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of...

Page 1: The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of Asynchrony Pros Cons 23 •Two things are necessary for the pros of asynchrony to outweigh

The Pros & Cons of Asynchrony

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: The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of Asynchrony Pros Cons 23 •Two things are necessary for the pros of asynchrony to outweigh

2

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

by understanding the pros & consof synchrony

• Motivate the need for Java futuresby understanding the pros & consof asynchrony

Page 3: The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of Asynchrony Pros Cons 23 •Two things are necessary for the pros of asynchrony to outweigh

3

Overview of Asynchrony &Asynchronous Operations

Page 4: The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of Asynchrony Pros Cons 23 •Two things are necessary for the pros of asynchrony to outweigh

4

• Asynchronous operations can alleviate limitations with synchronous operations

Overview of Asynchrony & Asynchronous Operations

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

Page 5: The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of Asynchrony Pros Cons 23 •Two things are necessary for the pros of asynchrony to outweigh

5See en.wikipedia.org/wiki/Asynchronous_method_invocation

• Asynchronous operations can alleviate limitations with synchronous operations

• Asynchrony is a means of concurrent programming where the caller does not block while waiting for the called code to finish

Overview of Asynchrony & Asynchronous Operations

Page 6: The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of Asynchrony Pros Cons 23 •Two things are necessary for the pros of asynchrony to outweigh

6

• Asynchronous operations can alleviate limitations with synchronous operations

• Asynchrony is a means of concurrent programming where the caller does not block while waiting for the called code to finish, e.g.

• Android AsyncTasks execute long-duration operations asynchronously in one or more background threads

Overview of Asynchrony & Asynchronous Operations

Background thread

See developer.android.com/reference/android/os/AsyncTask

Page 7: The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of Asynchrony Pros Cons 23 •Two things are necessary for the pros of asynchrony to outweigh

7

• Asynchronous operations can alleviate limitations with synchronous operations

• Asynchrony is a means of concurrent programming where the caller does not block while waiting for the called code to finish, e.g.

• Android AsyncTasks execute long-duration operations asynchronously in one or more background threads

• The caller (UI) thread can be notifiedupon completion, failure, or progressof the background task

Overview of Asynchrony & Asynchronous Operations

Calling thread

See developer.android.com/reference/android/os/AsyncTask

Page 8: The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of Asynchrony Pros Cons 23 •Two things are necessary for the pros of asynchrony to outweigh

8

The Pros of Asynchrony

Page 9: The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of Asynchrony Pros Cons 23 •Two things are necessary for the pros of asynchrony to outweigh

9

• Pros of asynchronous operations

The Pros of Asynchrony

Page 10: The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of Asynchrony Pros Cons 23 •Two things are necessary for the pros of asynchrony to outweigh

10

• Pros of asynchronous operations

• Responsiveness

• A calling thread needn’t block waiting for the async request to complete

The Pros of Asynchrony

See en.wikipedia.org/wiki/Asynchronous_method_invocation

Page 11: The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of Asynchrony Pros Cons 23 •Two things are necessary for the pros of asynchrony to outweigh

11

• Pros of asynchronous operations

• Responsiveness

• Elasticity

• Multiple requests can run scalably& concurrently on multiple cores

The Pros of Asynchrony

See en.wikipedia.org/wiki/Elasticity_(cloud_computing)

Page 12: The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of Asynchrony Pros Cons 23 •Two things are necessary for the pros of asynchrony to outweigh

12

• Pros of asynchronous operations

• Responsiveness

• Elasticity

• Multiple requests can run scalably& concurrently on multiple cores

• Elasticity is particularly usefulto auto-scale computationsin cloud environments

The Pros of Asynchrony

See en.wikipedia.org/wiki/Elasticity_(cloud_computing) & en.wikipedia.org/wiki/Autoscaling

Page 13: The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of Asynchrony Pros Cons 23 •Two things are necessary for the pros of asynchrony to outweigh

13

The Cons of Asynchrony

Page 14: The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of Asynchrony Pros Cons 23 •Two things are necessary for the pros of asynchrony to outweigh

14

• Cons of asynchronous operations

The Cons of Asynchrony

Page 15: The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of Asynchrony Pros Cons 23 •Two things are necessary for the pros of asynchrony to outweigh

15

• Cons of asynchronous operations

• Unpredictability

• Response times may not unpredictable dueto non-determinism of async operations

The Cons of Asynchrony

Non-determinism is a general problem with concurrency & not just asynchrony

Page 16: The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of Asynchrony Pros Cons 23 •Two things are necessary for the pros of asynchrony to outweigh

16

• Cons of asynchronous operations

• Unpredictability

• Response times may not unpredictable dueto non-determinism of async operations

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

The Cons of Asynchrony

CALLER CALLEE

searchForWord1

future result1

searchForWord2

future result3

searchForWord3

future result2

future1

future2

future3

future result1

future result3

future result2

Additional time & effort may be required if results must be ordered somehow

Page 17: The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of Asynchrony Pros Cons 23 •Two things are necessary for the pros of asynchrony to outweigh

17

• Cons of asynchronous operations

• Unpredictability

• Complicated programming & debugging

The Cons of Asynchrony

Page 18: The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of Asynchrony Pros Cons 23 •Two things are necessary for the pros of asynchrony to outweigh

18

• Cons of asynchronous operations

• Unpredictability

• Complicated programming & debugging

• The patterns & best-practicesof asynchronous programmingare not well understood

The Cons of Asynchrony

See dzone.com/articles/callback-hell

Page 19: The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of Asynchrony Pros Cons 23 •Two things are necessary for the pros of asynchrony to outweigh

19

• Cons of asynchronous operations

• Unpredictability

• Complicated programming & debugging

• The patterns & best-practicesof asynchronous programmingare not well understood

• Errors can be hard to trackdue to unpredictability

The Cons of Asynchrony

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

Page 20: The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of Asynchrony Pros Cons 23 •Two things are necessary for the pros of asynchrony to outweigh

20

• Cons of asynchronous operations

• Unpredictability

• Complicated programming & debugging

• The patterns & best-practicesof asynchronous programmingare not well understood

• Errors can be hard to trackdue to unpredictability

The Cons of Asynchrony

Again, this non-determinism is a general problem with concurrent processing

Page 21: The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of Asynchrony Pros Cons 23 •Two things are necessary for the pros of asynchrony to outweigh

21

Weighing the Pros & Cons of Asynchrony

Page 22: The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of Asynchrony Pros Cons 23 •Two things are necessary for the pros of asynchrony to outweigh

22

• Two things are necessary for the pros of asynchrony to outweigh the cons

Weighing the Pros & Cons of Asynchrony

Pros

Cons

Page 23: The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of Asynchrony Pros Cons 23 •Two things are necessary for the pros of asynchrony to outweigh

23

• Two things are necessary for the pros of asynchrony to outweigh the cons

• Performance should improve tooffset the increased complexity of programming & debugging

Weighing the Pros & Cons of Asynchrony

Performance

Productivity

See upcoming lesson on “Java Completable Futures ImageStreamGang Example”

Page 24: The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of Asynchrony Pros Cons 23 •Two things are necessary for the pros of asynchrony to outweigh

24

• Two things are necessary for the pros of asynchrony to outweigh the cons

• Performance should improve tooffset the increased complexity of programming & debugging

• An asynchronous programming model should reflect the keyprinciples of the reactive paradigm

Weighing the Pros & Cons of Asynchrony

See earlier lesson on “Overview of Reactive Programming”

Responsive

Resilient

Message-driven

Elastic

Page 25: The Pros & Cons of Asynchronyschmidt/cs891f/2019-PDFs/12.3.2...Weighing the Pros & Cons of Asynchrony Pros Cons 23 •Two things are necessary for the pros of asynchrony to outweigh

25

End of the Pros & Cons of Asynchrony