android concurrency frameworks: structure & …schmidt/cs891s/2019-pdfs/12.3.3...•these...

22
Android Concurrency Frameworks: Structure & Functionality Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Upload: others

Post on 22-May-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Android Concurrency Frameworks: Structure & …schmidt/cs891s/2019-PDFs/12.3.3...•These framework elements are used by Android’s application frameworks & packaged/3rd-party applications

Android Concurrency Frameworks:

Structure & Functionality

Douglas C. [email protected]

www.dre.vanderbilt.edu/~schmidt

Institute for Software

Integrated Systems

Vanderbilt University

Nashville, Tennessee, USA

Page 2: Android Concurrency Frameworks: Structure & …schmidt/cs891s/2019-PDFs/12.3.3...•These framework elements are used by Android’s application frameworks & packaged/3rd-party applications

2

Learning Objectives in this Part of the Lesson• Know the motivations for

Android concurrency & concurrency frameworks

• Recognize the two types of Android concurrency frameworks

• Understand the structure & functionality of Android’s concurrency frameworks

Looper

FutureTask

MessageQueue

Handler

Runnable

Executor

Message

……

Page 3: Android Concurrency Frameworks: Structure & …schmidt/cs891s/2019-PDFs/12.3.3...•These framework elements are used by Android’s application frameworks & packaged/3rd-party applications

3

Elements of Android Concurrency

Frameworks

Page 4: Android Concurrency Frameworks: Structure & …schmidt/cs891s/2019-PDFs/12.3.3...•These framework elements are used by Android’s application frameworks & packaged/3rd-party applications

4

• Android’s concurrency frameworks are built using reusable classes & interfaces

Elements of Android Concurrency Frameworks

Message

Message

Message

Message

Message

Message

Queue

UI Thread(main thread)

Message

Runnable

Message

Background

Thread A

Handler

Handler

Background

Thread B

Lo

op

er

AsyncTask

Executor

Handler

Fu

ture

Ta

sk

Page 5: Android Concurrency Frameworks: Structure & …schmidt/cs891s/2019-PDFs/12.3.3...•These framework elements are used by Android’s application frameworks & packaged/3rd-party applications

5

• Android’s concurrency frameworks are built using reusable classes & interfaces

Message

Message

Message

Message

Message

Message

Queue

Message

Lo

op

er

UI Thread(main thread)

Runnable

Message

Background

Thread A

Handler

Handler

Background

Thread B

AsyncTask

Executor

Handler

Fu

ture

Ta

sk

We focus on classes/interfaces used to write concurrent Android programs

Elements of Android Concurrency Frameworks

Page 6: Android Concurrency Frameworks: Structure & …schmidt/cs891s/2019-PDFs/12.3.3...•These framework elements are used by Android’s application frameworks & packaged/3rd-party applications

6

• Android’s concurrency frameworks are built using reusable classes & interfaces

Message

Message

Message

Message

Message

Message

Queue

Message

Lo

op

er

UI Thread(main thread)

Runnable

Message

Background

Thread A

Handler

Handler

Background

Thread B

AsyncTask

Executor

Handler

Fu

ture

Ta

sk

We’ll also outline the implementation of Android’s concurrency frameworks

Elements of Android Concurrency Frameworks

Page 7: Android Concurrency Frameworks: Structure & …schmidt/cs891s/2019-PDFs/12.3.3...•These framework elements are used by Android’s application frameworks & packaged/3rd-party applications

7

• Android’s concurrency frameworks are built using reusable classes & interfaces

• Looper

• Run a message loop for a thread

Message

Message

Message

Message

Message

Message

Queue

UI Thread(main thread)

Message

See developer.android.com/reference/android/os/Looper.html

Lo

op

er

Runnable

Message

Background

Thread A

Handler

Handler

Background

Thread B

Elements of Android Concurrency Frameworks

public static void loop() {

final Looper me =

myLooper();

MessageQueue queue =

me.mQueue;

...

for (;;) {

Message msg =

queue.next();

...

msg.target.

dispatchMessage(msg);

...

} ...

Page 8: Android Concurrency Frameworks: Structure & …schmidt/cs891s/2019-PDFs/12.3.3...•These framework elements are used by Android’s application frameworks & packaged/3rd-party applications

8

• Android’s concurrency frameworks are built using reusable classes & interfaces

• Looper

• MessageQueue

• Holds the list of messages to be dispatched by a looper

Lo

op

er Message

Message

Message

Message

Message

Message

Queue

UI Thread(main thread)

Message

See developer.android.com/reference/android/os/MessageQueue.html

Runnable

Message

Background

Thread A

Handler

Handler

Background

Thread B

Elements of Android Concurrency Frameworks

Page 9: Android Concurrency Frameworks: Structure & …schmidt/cs891s/2019-PDFs/12.3.3...•These framework elements are used by Android’s application frameworks & packaged/3rd-party applications

9

• Android’s concurrency frameworks are built using reusable classes & interfaces

• Looper

• MessageQueue

• Holds the list of messages to be dispatched by a looper

• Messages aren’t added directly to a message queue, but rather via handler objects associated with the looper

Lo

op

er Message

Message

Message

Message

Message

Message

Queue

UI Thread(main thread)

Message

Runnable

Message

Background

Thread A

Handler

Handler

Background

Thread B

Elements of Android Concurrency Frameworks

Page 10: Android Concurrency Frameworks: Structure & …schmidt/cs891s/2019-PDFs/12.3.3...•These framework elements are used by Android’s application frameworks & packaged/3rd-party applications

10

• Android’s concurrency frameworks are built using reusable classes & interfaces

• Looper

• MessageQueue

• Holds the list of messages to be dispatched by a looper

• Messages aren’t added directly to a message queue, but rather via handler objects associated with the looper

• The looper blocks on the message queue until thenext message is available

Lo

op

er Message

Message

Message

Message

Message

Message

Queue

UI Thread(main thread)

Message

Runnable

Message

Background

Thread A

Handler

Handler

Background

Thread B

Elements of Android Concurrency Frameworks

Page 11: Android Concurrency Frameworks: Structure & …schmidt/cs891s/2019-PDFs/12.3.3...•These framework elements are used by Android’s application frameworks & packaged/3rd-party applications

11

• Android’s concurrency frameworks are built using reusable classes & interfaces

• Looper

• MessageQueue

• Message

• Contains data & type information that can be sent to a handler via a message queue L

oo

pe

r

Message

Queue

UI Thread(main thread)

Message

See developer.android.com/reference/android/os/Message.html

Message

Message

Message

Message

Message

Runnable

Message

Background

Thread A

Handler

Handler

Background

Thread B

Elements of Android Concurrency Frameworks

Page 12: Android Concurrency Frameworks: Structure & …schmidt/cs891s/2019-PDFs/12.3.3...•These framework elements are used by Android’s application frameworks & packaged/3rd-party applications

12

• Android’s concurrency frameworks are built using reusable classes & interfaces

• Looper

• MessageQueue

• Message

• Handler

• Send/process messages & runnables in the message queue associated with a thread’s looper

Lo

op

er Message

Message

Message

Message

Message

Message

Queue

UI Thread(main thread)

Message

See developer.android.com/reference/android/os/Handler.html

Runnable

Message

Background

Thread A

Handler

Handler

Background

Thread B

Elements of Android Concurrency Frameworks

Page 13: Android Concurrency Frameworks: Structure & …schmidt/cs891s/2019-PDFs/12.3.3...•These framework elements are used by Android’s application frameworks & packaged/3rd-party applications

13

• Android’s concurrency frameworks are built using reusable classes & interfaces

• Looper

• MessageQueue

• Message

• Handler

• Send/process messages & runnables in the message queue associated with a thread’s looper

• Plays the roles of a proxy for clientthread & of a target adapter for dispatching in another thread

Lo

op

er Message

Message

Message

Message

Message

Message

Queue

UI Thread(main thread)

Message

See en.wikipedia.org/wiki/Proxy_pattern & en.wikipedia.org/wiki/Adapter_pattern

Runnable

Message

Background

Thread A

Handler

Handler

Background

Thread B

Elements of Android Concurrency Frameworks

Page 14: Android Concurrency Frameworks: Structure & …schmidt/cs891s/2019-PDFs/12.3.3...•These framework elements are used by Android’s application frameworks & packaged/3rd-party applications

14

• Android’s concurrency frameworks are built using reusable classes & interfaces

• Looper

• MessageQueue

• Message

• Handler

• Runnable

• Represents a command that can be executed

Lo

op

er Message

Message

Message

Message

Message

Message

Queue

UI Thread(main thread)

Message

See developer.android.com/reference/java/lang/Runnable.html

Runnable

Message

Background

Thread A

Handler

Handler

Background

Thread B

Elements of Android Concurrency Frameworks

Page 15: Android Concurrency Frameworks: Structure & …schmidt/cs891s/2019-PDFs/12.3.3...•These framework elements are used by Android’s application frameworks & packaged/3rd-party applications

15

• Android’s concurrency frameworks are built using reusable classes & interfaces

• Looper

• MessageQueue

• Message

• Handler

• Runnable

• Represents a command that can be executed

• This command is often runin a thread different than itwas created in

Lo

op

er Message

Message

Message

Message

Message

Message

Queue

UI Thread(main thread)

Message

See www.dre.vanderbilt.edu/~schmidt/PDF/CommandProcessor.pdf

Runnable

Message

Background

Thread A

Handler

Handler

Background

Thread B

Elements of Android Concurrency Frameworks

Page 16: Android Concurrency Frameworks: Structure & …schmidt/cs891s/2019-PDFs/12.3.3...•These framework elements are used by Android’s application frameworks & packaged/3rd-party applications

16

• Android’s concurrency frameworks are built using reusable classes & interfaces

• Looper

• MessageQueue

• Message

• Handler

• Runnable

Lo

op

er Message

Message

Message

Message

Message

Message

Queue

UI Thread(main thread)

Message

These classes are used by both the HaMeR & AsyncTask

concurrency frameworks

Elements of Android Concurrency Frameworks

Runnable

Message

Background

Thread A

Handler

Handler

Background

Thread B

The HaMeR framework exposes some classes to app developers directly, whereas the AsyncTask framework shields app developers from these classes

Page 17: Android Concurrency Frameworks: Structure & …schmidt/cs891s/2019-PDFs/12.3.3...•These framework elements are used by Android’s application frameworks & packaged/3rd-party applications

17

• Android’s concurrency frameworks are built using reusable classes & interfaces

• Looper

• MessageQueue

• Message

• Handler

• Runnable

Lo

op

er Message

Message

Message

Message

Message

Message

Queue

UI Thread(main thread)

Message

Some classes are just used by the AsyncTask concurrency framework

Elements of Android Concurrency Frameworks

AsyncTask

Executor

Handler

Fu

ture

Ta

sk

Page 18: Android Concurrency Frameworks: Structure & …schmidt/cs891s/2019-PDFs/12.3.3...•These framework elements are used by Android’s application frameworks & packaged/3rd-party applications

18

• Android’s concurrency frameworks are built using reusable classes & interfaces

• Looper

• MessageQueue

• Message

• Handler

• Runnable

• FutureTask

• Can be used to

• Start & cancel a computation that runs asynchronously

• Query to see if acomputation is done

• Retrieve the result of the computation

Lo

op

er Message

Message

Message

Message

Message

Message

Queue

UI Thread(main thread)

Message

AsyncTask

Executor

Handler

See developer.android.com/reference/java/util/concurrent/FutureTask.html

Fu

ture

Ta

sk

Elements of Android Concurrency Frameworks

Page 19: Android Concurrency Frameworks: Structure & …schmidt/cs891s/2019-PDFs/12.3.3...•These framework elements are used by Android’s application frameworks & packaged/3rd-party applications

19

AsyncTask

Executor

• Android’s concurrency frameworks are built using reusable classes & interfaces

• Looper

• MessageQueue

• Message

• Handler

• Runnable

• FutureTask

• Executor framework

• Execute submitted runnable tasks either

• Sequentially in one thread (in the background) or

• Concurrently in a thread pool

Lo

op

er Message

Message

Message

Message

Message

Message

Queue

UI Thread(main thread)

Message

Fu

ture

Ta

sk Handler

See developer.android.com/reference/java/util/concurrent/Executor.html

Elements of Android Concurrency Frameworks

Page 20: Android Concurrency Frameworks: Structure & …schmidt/cs891s/2019-PDFs/12.3.3...•These framework elements are used by Android’s application frameworks & packaged/3rd-party applications

20

• These framework elements are used by Android’s application frameworks & packaged/3rd-party applications

Additional Application Frameworks

Operating System Kernel

Applications

System Libraries

Java Virtual Machine

Threading & Synchronization Packages

Elements of Android Concurrency Frameworks

See source.android.com

Page 21: Android Concurrency Frameworks: Structure & …schmidt/cs891s/2019-PDFs/12.3.3...•These framework elements are used by Android’s application frameworks & packaged/3rd-party applications

21

End of Android Concurrency Frameworks: Structure &

Functionality

Page 22: Android Concurrency Frameworks: Structure & …schmidt/cs891s/2019-PDFs/12.3.3...•These framework elements are used by Android’s application frameworks & packaged/3rd-party applications

22

1. Which of the following class/interface elements are used by both the HaMeR framework & the AsyncTask framework?

a. Executor

b.MessageQueue

c. Runnable

d.Message

e. Handler

f. FutureTask

g. Looper

Discussion Questions