![Page 1: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/1.jpg)
Overview of Android Concurrency Frameworks (Part 1)
Douglas C. [email protected]
www.dre.vanderbilt.edu/~schmidt
Institute for Software Integrated Systems
Vanderbilt University Nashville, Tennessee, USA
![Page 2: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/2.jpg)
2
Learning Objectives in this Part of the Lesson• Know the motivations for Android concurrency & concurrency frameworks
![Page 3: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/3.jpg)
15
Motivating Android’s Concurrency Frameworks
![Page 4: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/4.jpg)
16See developer.android.com/guide/components/
processes-and-threads.html#Threads
Motivating Android Concurrency Frameworks• Android’s concurrency frameworks
also address design constraints
![Page 5: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/5.jpg)
17
Motivating Android Concurrency Frameworks• Android’s concurrency frameworks
also address design constraints, e.g.• “ANR” dialog is generated if the
UI thread blocks too long
See developer.android.com/training/articles/perf-anr.html
The UI thread can’t block for more than several seconds, so it can’t be used for long-duration operations
![Page 6: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/6.jpg)
18See blog.vogella.com/2012/02/22/android-strictmode-networkonmainthreadexception
Motivating Android Concurrency Frameworks• Android’s concurrency frameworks
also address design constraints, e.g.• “ANR” dialog is generated if the
UI thread blocks too long• Network calls are disallowed on
the UI thread by default
![Page 7: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/7.jpg)
19See android-developers.blogspot.com/2009/05/painless-threading.html
• Android’s concurrency frameworks also address design constraints, e.g.• “ANR” dialog is generated if the
UI thread blocks too long• Network calls are disallowed on
the UI thread by default• Non-UI threads can’t access
UI toolkit components directly
Motivating Android Concurrency Frameworks
UI toolkit components aren’t thread-safe
![Page 8: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/8.jpg)
20
• Android’s concurrency frameworks also address design constraints, e.g.• “ANR” dialog is generated if the
UI thread blocks too long• Network calls are disallowed on
the UI thread by default• Non-UI threads can’t access
UI toolkit components directly
See www.dre.vanderbilt.edu/~schmidt/LiveLessons/CPiJava
Motivating Android Concurrency Frameworks
Additional Application Frameworks
Operating System Kernel
Applications
System Libraries
Java Virtual Machine
Threading & Synchronization Packages
Java concurrency mechanisms alone cannot address these constraints!!
![Page 9: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/9.jpg)
21
• Android’s concurrency frameworks also address design constraints, e.g.• “ANR” dialog is generated if the
UI thread blocks too long• Network calls are disallowed on
the UI thread by default• Non-UI threads can’t access
UI toolkit components directly
Motivating Android Concurrency Frameworks
Android concurrency frameworks address these design constraints
See developer.android.com/guide/components/processes-and-threads.html#WorkerThreads
Loop
er Message
Message
Message
Message
Message
Message Queue
Message
UI Thread(main thread)
Runnable
Message
Background Thread A
Handler
Handler
Background Thread B
AsyncTask
Executor
Handler
Futu
reTa
sk
![Page 10: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/10.jpg)
22
Motivating Android Concurrency Frameworks• The “Buggy Downloader” app motivates the
need for Android’s concurrency frameworks
See github.com/douglascraigschmidt/POSA/tree/master/ex/M4/BuggyDownloader
![Page 11: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/11.jpg)
23
Motivating Android Concurrency Frameworks• The “Buggy Downloader” app motivates the
need for Android’s concurrency frameworks• “Buggy1” throws an exception since the
image is downloaded in the UI thread
![Page 12: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/12.jpg)
24
Motivating Android Concurrency Frameworks• The “Buggy Downloader” app motivates the
need for Android’s concurrency frameworks• “Buggy1” throws an exception since the
image is downloaded in the UI thread• “Buggy2” throws an exception since a UI
component is accessed via a background thread
![Page 13: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/13.jpg)
25
End of Overview of Android Concurrency Frameworks
(Part 1)
![Page 14: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/14.jpg)
Overview of Android Concurrency Frameworks (Part 2)
Douglas C. [email protected]
www.dre.vanderbilt.edu/~schmidt
Institute for Software Integrated Systems
Vanderbilt University Nashville, Tennessee, USA
![Page 15: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/15.jpg)
2
Learning Objectives in this Part of the Lesson• Know the motivations for
Android concurrency & concurrency frameworks
• Recognize the structure & functionality of Android’s concurrency frameworks
Loop
er Message
Message
Message
Message
Message
Message Queue
Message
UI Thread(main thread)
Runnable
Message
Background Thread A
Handler
Handler
Background Thread B
AsyncTask
Executor
Handler
Futu
reTa
sk
![Page 16: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/16.jpg)
3
Learning Objectives in this Part of the Lesson• Know the motivations for
Android concurrency & concurrency frameworks
• Recognize the structure & functionality of Android’s concurrency frameworks, e.g.• Handler, Messages, & Runnables
(HaMeR) framework
See code.tutsplus.com/tutorials/concurrency-on-android-using-hamer-framework--cms-27129
Loop
er Message
Message
Message
Message
Message
Message Queue
Message
Runnable
Message
Background Thread A
Handler
Handler
Background Thread B
![Page 17: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/17.jpg)
4
Learning Objectives in this Part of the Lesson• Know the motivations for
Android concurrency & concurrency frameworks
• Recognize the structure & functionality of Android’s concurrency frameworks, e.g.• Handler, Messages, & Runnables
(HaMeR) framework• AsyncTask framework
See developer.android.com/reference/android/os/AsyncTask.html
Loop
er Message
Message
Message
Message
Message
Message Queue
Message
UI Thread(main thread) AsyncTask
Executor
Handler
Futu
reTa
sk
![Page 18: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/18.jpg)
5
Overview of Android Concurrency
Frameworks
![Page 19: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/19.jpg)
6
• Android defines two primary concurrency frameworks
Overview of Android Concurrency Frameworks
Loop
er Message
Message
Message
Message
Message
Message Queue
Message
UI Thread(main thread) AsyncTask
Executor
Runnable
Message
Background Thread A
Handler
Handler
Background Thread B
![Page 20: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/20.jpg)
7
• Android defines two primary concurrency frameworks • Handlers, Messages, & Runnables
(HaMeR)
Loop
er Message
Message
Message
Message
Message
Message Queue
Message
Overview of Android Concurrency Frameworks
See developer.android.com/training/multiple-threads/communicate-ui.html
Operations running in one or more background threads can post/send
their results to the UI thread
UI Thread(main thread)
Runnable
Message
Background Thread A
Handler
Handler
Background Thread B
![Page 21: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/21.jpg)
8
• Android defines two primary concurrency frameworks • Handlers, Messages, & Runnables
(HaMeR)• AsyncTask
Loop
er Message
Message
Message
Message
Message
Message Queue
Message
Overview of Android Concurrency Frameworks
AsyncTask
ExecutorFutu
reTa
sk HandlerOperations run in one or more
background threads & publish results to UI thread without manipulating threads,
handlers, messages, or runnables
UI Thread(main thread)
See developer.android.com/reference/android/os/AsyncTask.html
![Page 22: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/22.jpg)
9
• Both frameworks have pros &cons & are used extensivelythroughout Android
Overview of Android Concurrency Frameworks
See upcoming part on “Evaluating Android’s Concurrency Frameworks”
AsyncTask
PostingRunnables
SendingMessages
Usability(Simple)
+++ +++ ++
Usability(Complex)
+++ + ++
Scalability +++ + +Flexibility ++ + +++Efficiency ++ +++ +++
![Page 23: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/23.jpg)
10
Overview of Android Concurrency Frameworks
Long-duration & (potentially) blocking operations run in
background thread(s)
• Android’s concurrency frameworks are often used to decouple user interactions from computation & communication
![Page 24: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/24.jpg)
11
Overview of Android Concurrency FrameworksShort-duration, user-facing
operations run in the UI thread• Android’s concurrency frameworks are
often used to decouple user interactions from computation & communication
![Page 25: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/25.jpg)
12
Message Queue
• Android’s concurrency frameworks are often used to decouple user interactions from computation & communication
Overview of Android Concurrency Frameworks
Loop
er Message
Message
Message
Message
Message
MessageUI Thread(main thread)
Runnable
Message
Background Thread A
Handler
Handler
Background Thread B
See github.com/douglascraigschmidt/POSA/tree/master/ex/M4/SimpleImageDownloads
![Page 26: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/26.jpg)
13
Background threads perform long-duration
image downloads
Message Queue
• Android’s concurrency frameworks are often used to decouple user interactions from computation & communication
Overview of Android Concurrency Frameworks
Loop
er Message
Message
Message
Message
Message
MessageUI Thread(main thread)
Runnable
Message
Background Thread A
Handler
Handler
Background Thread B
![Page 27: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/27.jpg)
14
Message Queue
• Android’s concurrency frameworks are often used to decouple user interactions from computation & communication
Overview of Android Concurrency Frameworks
Synchronized message queue passes results
from background thread(s) to UI thread
Loop
er Message
Message
Message
Message
Message
MessageUI Thread(main thread)
Runnable
Message
Background Thread A
Handler
Handler
Background Thread B
![Page 28: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/28.jpg)
15
• Android’s concurrency frameworks are often used to decouple user interactions from computation & communication
Overview of Android Concurrency Frameworks
UI thread displays the image to the user
Loop
er Message
Message
Message
Message
Message
Message Queue
MessageUI Thread(main thread)
Runnable
Message
Background Thread A
Handler
Handler
Background Thread B
![Page 29: Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2. Learning Objectives in this Part of the Lesson • Know the motivations for Android](https://reader033.vdocuments.us/reader033/viewer/2022042311/5ed9f5f228db2d5ca24927ef/html5/thumbnails/29.jpg)
16
End of Overview of the Android Concurrency Frameworks (Part 2)