android concurrency frameworks: structure & …schmidt/cs891s/2019-pdfs/12.3.3...•these...
TRANSCRIPT
Android Concurrency Frameworks:
Structure & Functionality
Douglas C. [email protected]
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 two types of Android concurrency frameworks
• Understand the structure & functionality of Android’s concurrency frameworks
Looper
FutureTask
MessageQueue
Handler
Runnable
Executor
Message
……
3
Elements of Android Concurrency
Frameworks
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
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
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
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);
...
} ...
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
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
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
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
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
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
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
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
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
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
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
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
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
21
End of Android Concurrency Frameworks: Structure &
Functionality
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