Beruflich Dokumente
Kultur Dokumente
d.schmidt@vanderbilt.edu
www.dre.vanderbilt.edu/~schmidt
Institute for Software
Integrated Systems
Vanderbilt University
Nashville, Tennessee, USA
CS 282 Principles of Operating Systems II
Systems Programming for Android
Android Concurrency
Douglas C. Schmidt
Android Concurrency
Douglas C. Schmidt
Android Concurrency
Douglas C. Schmidt
Android Concurrency
Douglas C. Schmidt
Background
Thread 1
Socket
Socket
UI Thread
(main thread)
Android Concurrency
Douglas C. Schmidt
asynchronous
Background
Thread11
Background
Thread22
Background
Thread33
UI Thread
Looper
Android Concurrency
Douglas C. Schmidt
asynchronous
Background
Thread2
Background
Thread3
<<take>>
<<take>>
BlockingQueue
AsyncTask framework
<<take>>
<<offer>>
<<execute>>
<<next>>
MyActivity
UI
Thread
UI Thread
Looper
Android Concurrency
Half-Sync/Half-Async
Douglas C. Schmidt
POSA2 Concurrency
Intent
Decouple asynchronous (async) & synchronous (sync) service processing in
concurrent systems by introducing two intercommunicating layersone for
async & one for sync service processingto simplify programming without
unduly reducing performance
8
www.dre.vanderbilt.edu/~schmidt/PDF/HS-HA.pdf
has more info
Android Concurrency
Douglas C. Schmidt
Half-Sync/Half-Async
POSA2 Concurrency
Applicability
When its necessary to make performance efficient & scalable, while also
ensuring that the use of concurrency simplifiesrather than complicates
programming
Android Concurrency
Half-Sync/Half-Async
Douglas C. Schmidt
POSA2 Concurrency
Applicability
When its necessary to make performance efficient & scalable, while also
ensuring that the use of concurrency simplifiesrather than complicates
programming
When there are constraints on certain types of operations in certain contexts
e.g., short-duration vs. long-duration, blocking vs. non-blocking, etc.
Android Concurrency
Douglas C. Schmidt
Half-Sync/Half-Async
POSA2 Concurrency
UI Thread
11
Android Concurrency
Douglas C. Schmidt
Half-Sync/Half-Async
POSA2 Concurrency
Message
Queue
12
Android Concurrency
Douglas C. Schmidt
Half-Sync/Half-Async
POSA2 Concurrency
Background threads
13
Android Concurrency
Douglas C. Schmidt
Half-Sync/Half-Async
POSA2 Concurrency
Dynamics
Event handling
runs reactively/
asynchronously
14
Android Concurrency
Douglas C. Schmidt
Half-Sync/Half-Async
POSA2 Concurrency
Dynamics
Queue requests
without blocking caller
15
Android Concurrency
Half-Sync/Half-Async
Douglas C. Schmidt
POSA2 Concurrency
Dynamics
Sync services run concurrently, relative16both to each other & to async services
Android Concurrency
Douglas C. Schmidt
Half-Sync/Half-Async
POSA2 Concurrency
Consequences
+ Simplification & performance
Programming of higher-level sync
processing services are simplified
without degrading performance of
lower-level system services
4. doInBackGround()
Message
Queue
Executor
Looper
5. onProgressUpdate()
Message
6. onPostExecute()
Message
3. execute(future)
Message
Message
Message
Message
Handler
2. onPreExecute()
AsyncTask
1. execute(url)
UI Thread
(main thread)
17
Android Concurrency
Douglas C. Schmidt
Half-Sync/Half-Async
POSA2 Concurrency
Consequences
+ Simplification & performance
+ Separation of concerns
Synchronization policies in each
layer are decoupled so that each
layer need not use the same
concurrency strategies
4. doInBackGround()
Message
Queue
Executor
Looper
5. onProgressUpdate()
Message
6. onPostExecute()
Message
3. execute(future)
Message
Message
Message
Message
Handler
2. onPreExecute()
AsyncTask
1. execute(url)
UI Thread
(main thread)
18
Android Concurrency
Douglas C. Schmidt
Half-Sync/Half-Async
4. doInBackGround()
Message
Queue
Executor
5. onProgressUpdate()
Looper
Consequences
+ Simplification & performance
+ Separation of concerns
+ Centralization of inter-layer
communication
Inter-layer communication
is centralized because all
interaction is mediated by
the queueing layer
POSA2 Concurrency
Message
6. onPostExecute()
Message
3. execute(future)
Message
Message
Message
Message
Handler
2. onPreExecute()
AsyncTask
1. execute(url)
UI Thread
(main thread)
19
Android Concurrency
Douglas C. Schmidt
Half-Sync/Half-Async
POSA2 Concurrency
Looper
Consequences
May incur a boundary-crossing penalty
Arising from context switching,
synchronization, & data copying
overhead when data transferred
between sync & async service
layers via queueing layer
4. doInBackGround()
Message
Queue
Executor
5. onProgressUpdate()
Message
6. onPostExecute()
Message
3. execute(future)
Message
Message
Message
Message
Handler
2. onPreExecute()
AsyncTask
1. execute(url)
UI Thread
(main thread)
20
Android Concurrency
Douglas C. Schmidt
Half-Sync/Half-Async
POSA2 Concurrency
Looper
Consequences
May incur a boundary-crossing penalty
Higher-level app services may not
benefit from async I/O
Depending on design of OS or app
framework interfaces, higher-level
services may not use low-level
async I/O devices effectively
4. doInBackGround()
Message
Queue
Executor
5. onProgressUpdate()
Message
6. onPostExecute()
Message
3. execute(future)
Message
Message
Message
Message
Handler
2. onPreExecute()
AsyncTask
1. execute(url)
UI Thread
(main thread)
21
Android Concurrency
Douglas C. Schmidt
Half-Sync/Half-Async
POSA2 Concurrency
Looper
Consequences
May incur a boundary-crossing penalty
Higher-level app services may not
benefit from async I/O
Complexity of debugging & testing
Apps can be hard to debug due
to concurrent execution
4. doInBackGround()
Message
Queue
Executor
5. onProgressUpdate()
Message
6. onPostExecute()
Message
3. execute(future)
Message
Message
Message
Message
Handler
2. onPreExecute()
AsyncTask
1. execute(url)
UI Thread
(main thread)
22
Android Concurrency
Half-Sync/Half-Async
Douglas C. Schmidt
POSA2 Concurrency
Known Uses
UNIX Networking Subsystems
23
www.dre.vanderbilt.edu/~schmidt/PDF/HS-HA.pdf
has more info
Android Concurrency
Douglas C. Schmidt
Half-Sync/Half-Async
POSA2 Concurrency
Known Uses
UNIX Networking Subsystems
Object Request Brokers (ORBs)
Object
(Servant)
Lane
2
Lane
3
Object Adapter
<<ready to read>>
Android Concurrency
Douglas C. Schmidt
Half-Sync/Half-Async
POSA2 Concurrency
Known Uses
UNIX Networking Subsystems
Object Request Brokers (ORBs)
Android AsyncTask framework
4. doInBackGround()
Message
Queue
Executor
Looper
5. onProgressUpdate()
Message
6. onPostExecute()
Message
3. execute(future)
Message
Message
Message
Message
Handler
2. onPreExecute()
AsyncTask
1. execute(url)
UI Thread
(main thread)
25
developer.android.com/training/multiple-threads/communicate-ui.html
Android Concurrency
Douglas C. Schmidt
Summary
This pattern separates concerns between the three layers, which makes
concurrent software easier to understand, debug, & evolve
26
Android Concurrency
Douglas C. Schmidt
Summary
This pattern separates concerns between the three layers, which makes
concurrent software easier to understand, debug, & evolve
In addition, async & sync services do not suffer from each others liabilities
Async service performance does not degrade due to blocking sync services
27
Android Concurrency
Douglas C. Schmidt
Summary
This pattern separates concerns between the three layers, which makes
concurrent software easier to understand, debug, & evolve
In addition, async & sync services do not suffer from each others liabilities
Async service performance does not degrade due to blocking sync services
The simplicity of programming sync services is unaffected by async
complexities, such as explicit state management
28
Android Concurrency
Douglas C. Schmidt
Summary
This pattern separates concerns between the three layers, which makes
concurrent software easier to understand, debug, & evolve
In addition, async & sync services do not suffer from each others liabilities
The queueing layer avoids hard-coded dependencies between the async &
sync service layers
Its also easy to reprioritize the order in which messages are processed
29
Android Concurrency:
The Half-Sync/Half-Async Pattern (Part 2)
Douglas C. Schmidt
d.schmidt@vanderbilt.edu
www.dre.vanderbilt.edu/~schmidt
Institute for Software
Integrated Systems
Vanderbilt University
Nashville, Tennessee, USA
CS 282 Principles of Operating Systems II
Systems Programming for Android
Android Concurrency
Douglas C. Schmidt
Background
Thread2
Background
Thread3
<<take>>
<<take>>
BlockingQueue <<take>>
AsyncTask framework
<<offer>>
<<execute>>
<<next>>
MyActivity
UI
Thread
31
UI Thread
Looper
Android Concurrency
Half-Sync/Half-Async
Douglas C. Schmidt
POSA2 Concurrency
Implementation
Decompose overall system into three layers: synchronous, asynchronous, &
queueing
public abstract class AsyncTask<Params, Progress, Result> {
public final AsyncTask<Params, Progress, Result>
execute(Params... params) {
return executeOnExecutor(sDefaultExecutor, params);
}
public final AsyncTask<Params, Progress, Result>
executeOnExecutor(Executor exec, Params... params) {
onPreExecute();
mWorker.mParams = params;
Identify short-duration
exec.execute(mFuture);
services & implement
return this;
them in the async layer
}
...
32
frameworks/base/core/java/android/os/AsyncTask.java
has the source code
Android Concurrency
Half-Sync/Half-Async
Douglas C. Schmidt
POSA2 Concurrency
Implementation
Decompose overall system into three layers: synchronous, asynchronous, &
queueing
public abstract class AsyncTask<Params, Progress, Result> {
public AsyncTask() {
mWorker = new WorkerRunnable<Params, Result>() {
public Result call() throws Exception {
...
return postResult(doInBackground(mParams));
}
};
...
33
frameworks/base/core/java/android/os/AsyncTask.java
has the source code
Android Concurrency
Douglas C. Schmidt
Half-Sync/Half-Async
POSA2 Concurrency
Implementation
Decompose overall system into three layers: synchronous, asynchronous, &
queueing
public class ThreadPoolExecutor
extends AbstractExecutorService {
/**
* The queue used for holding tasks and handing off to worker
* threads. */
private final BlockingQueue<Runnable> workQueue;
34
frameworks/base/core/java/android/os/AsyncTask.java
has the source code
Android Concurrency
Douglas C. Schmidt
Half-Sync/Half-Async
POSA2 Concurrency
Implementation
Decompose overall system into three layers: synchronous, asynchronous, &
queueing
class DownloadAsyncTask extends
Implement the services
AsyncTask<String, Integer, Bitmap> {
in the synchronous layer
...
protected Bitmap
doInBackground(String... url) {
return downloadImage(url[0]);
}
Download in background thread
35
Android Concurrency
Douglas C. Schmidt
Half-Sync/Half-Async
POSA2 Concurrency
Implementation
Decompose overall system into three layers: synchronous, asynchronous, &
queueing
class DownloadAsyncTask extends
Implement the services
AsyncTask<String, Integer, Bitmap> {
in the synchronous layer
protected void onPreExecute() {
Implement the services
dialog.display();
in the asynchronous layer
Perform on
UI thread
protected void onPostExecute
(Bitmap bitmap) {
performPostDownloadOperations(bitmap);
dialog.dismiss();
}
}
Perform on
UI thread
}
36
Android Concurrency
Douglas C. Schmidt
Half-Sync/Half-Async
POSA2 Concurrency
Implementation
Decompose overall system into three layers: synchronous, asynchronous, &
queueing
public class ThreadPoolExecutor
Implement the services
extends AbstractExecutorService {
in the synchronous layer
...
Implement the services
private Runnable getTask() {
in the asynchronous layer
...
Runnable r = workQueue.take();
Implement (or reuse) the
...
queueing layer
return r;
...
public void execute(Runnable command) {
...
workQueue.offer(command);
...
37
frameworks/base/core/java/android/os/AsyncTask.java
has the source code
Android Concurrency
Douglas C. Schmidt
Half-Sync/Half-Async
POSA2 Concurrency
: Worker
Runnable
execute(url)
onPreExecute()
Worker
Threads
execute
offer()
take()
UI
Thread
onPostExecute()
UI Thread receives
user request to
download a particular
URL & calls execute()
38
call()
doInBackground()
postResult()
Android Concurrency
Douglas C. Schmidt
Half-Sync/Half-Async
POSA2 Concurrency
: Worker
Runnable
execute(url)
onPreExecute()
Worker
Threads
execute
offer()
take()
call()
UI
Thread
doInBackground()
postResult()
Android Concurrency
Douglas C. Schmidt
Half-Sync/Half-Async
POSA2 Concurrency
: Worker
Runnable
execute(url)
onPreExecute()
Worker
Threads
execute
offer()
take()
UI
Thread
onPostExecute()
call()
doInBackground()
postResult()
Android Concurrency
Douglas C. Schmidt
Half-Sync/Half-Async
POSA2 Concurrency
: Worker
Runnable
execute(url)
onPreExecute()
Worker
Threads
execute
offer()
take()
UI
Thread
onPostExecute()
call()
doInBackground()
postResult()
Android Concurrency
Douglas C. Schmidt
Summary
Background
Thread1
Background
Thread2
<<take>>
<<take>>
BlockingQueue
AsyncTask framework
<<take>>
<<offer>>
<<execute>>
<<next>>
MyActivity
UI
Thread
Background
Thread3
UI Thread
Looper
42
Android Concurrency
Douglas C. Schmidt
Summary
Background
Thread1
Background
Thread2
<<take>>
<<take>>
BlockingQueue
AsyncTask framework
<<next>>
MyActivity
UI
Thread
<<take>>
<<offer>>
<<execute>>
Background
Thread3
UI Thread
Looper