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

29
Overview of Android Concurrency Frameworks (Part 1) Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

16

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