Overview of Android Concurrency Frameworks (Part 1)schmidt/cs891s/2018-PDFs/03-Android-con… · 2....

Preview:

Citation preview

Overview of Android Concurrency Frameworks (Part 1)

Douglas C. Schmidtd.schmidt@vanderbilt.edu

www.dre.vanderbilt.edu/~schmidt

Institute for Software Integrated Systems

Vanderbilt University Nashville, Tennessee, USA

2

Learning Objectives in this Part of the Lesson• Know the motivations for Android concurrency & concurrency frameworks

15

Motivating Android’s Concurrency Frameworks

16See developer.android.com/guide/components/

processes-and-threads.html#Threads

Motivating Android Concurrency Frameworks• Android’s concurrency frameworks

also address design constraints

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

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

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

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!!

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

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

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

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

25

End of Overview of Android Concurrency Frameworks

(Part 1)

Overview of Android Concurrency Frameworks (Part 2)

Douglas C. Schmidtd.schmidt@vanderbilt.edu

www.dre.vanderbilt.edu/~schmidt

Institute for Software Integrated Systems

Vanderbilt University Nashville, Tennessee, USA

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

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

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

5

Overview of Android Concurrency

Frameworks

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

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

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

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 ++ +++ +++

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

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

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

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

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

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

16

End of Overview of the Android Concurrency Frameworks (Part 2)

Recommended