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
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
3
Overview of Asynchrony &Asynchronous Operations
4
• Asynchronous operations can alleviate limitations with synchronous operations
Overview of Asynchrony & Asynchronous Operations
See en.wikipedia.org/wiki/Asynchrony_(computer_programming)
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
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
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
8
The Pros of Asynchrony
9
• Pros of asynchronous operations
The Pros of Asynchrony
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
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)
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
13
The Cons of Asynchrony
14
• Cons of asynchronous operations
The Cons of Asynchrony
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
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
17
• Cons of asynchronous operations
• Unpredictability
• Complicated programming & debugging
The Cons of Asynchrony
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
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
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
21
Weighing the Pros & Cons of Asynchrony
22
• Two things are necessary for the pros of asynchrony to outweigh the cons
Weighing the Pros & Cons of Asynchrony
Pros
Cons
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”
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
25
End of the Pros & Cons of Asynchrony