The document discusses concurrency and asynchronous techniques in Android. It explains that Android runs on multi-core devices so concurrency is important. It covers Java concurrency primitives like threads and executors. For Android, it recommends approaches like HandlerThread, AsyncTask, IntentService and Loader which are firmly based on the Java concurrency library. It cautions that AsyncTask behavior has changed and tasks may not run sequentially. It also provides examples of when different asynchronous techniques would be best used.
2. Why is concurrency in Android
important?
• Until pretty recently, real, concurrent
execution happened only on expensive server
hardware
• – Android already runs on multi-core devices.
• – It will get worse: Moore's law is now about
number, not speed, of CPUs
• – Android devices actually need to do multiple
things at the same time: Real Time Computing
3.
4. Android Puts Concurrency
in your Face
• The UI is single threaded but will not tolerate
long running, synchronous tasks
• IPC (Binder) calls appear on non-UI threads
and you can't use the UI from a non-UI thread
(a topic for another day...)
5. Java Concurrency Primitives
• Language level constructs
– Synchronized
– Thread/Runnable
• Concurrency library (java.util.concurrent)
– Executor/Callable/Future
– Atomics
• All are available in Android
– Low level Java tools are really low level. If you find
yourself using them you might want to review your
architecture
• – Android Concurrency tools are firmly based on the
concurrency library (java.util.concurrent)
17. Android Scheduling
1. Foreground
2. Visible
3. Service
4. Background
Process level:
App A
App A
Foreground Thread Group
Background Thread Group
App BApp B
> 90%
< 10%
18. Android Scheduling
1. Foreground
2. Visible
3. Service
4. Background
Process level:
App A
App A
Foreground Thread Group
Background Thread Group
App BApp B
> 90%
< 10%
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
23. HandlerThread
• Inherits from Thread and encapsulates a
Looper-object
• Thread with a message queue and processing
loop
• Handles both Message and Runnable
Running Dead
t.quit()t = new HandlerThread().start();
24. How it works
HandlerThread
Handler
Thread
Create and Start
Add Process
Message Queue
1
23
t = new HandlerThread("BgThread");
t.start();
1
Handler h = new Handler(t.getLooper()) {
@Override
public void handleMessage(Message msg) {
//Process message
}
};
2
h.sendEmptyMessage(42);
3
28. Application Global Behavior
AsyncTask > Pitfalls
execute()
Queue Thread Pool
Activity
execute()Activity
execute()Service
execute()Receiver
execute()*
29. execute()
•Execution behavior has changed over time
AsyncTask > Pitfalls
execute()< Donut:
execute()< Honeycomb:
execute()
executeOnExecutor(Executor)
>= Honeycomb:
30. execute()
AsyncTask > Pitfalls
<uses-sdk android:targetSdkVersion="12" />
<uses-sdk android:targetSdkVersion="13" />
“So, if I call AsyncTask.execute on
Honeycomb and later my tasks will run
sequentially, right?”
“Right?!?”
“Eh, it depends…”
36. Weak Reference
• Just make all references to the Activity weak
• Currently a “well-known” solution
• The refuge of scoundrels:
– Allows the process to continue, even when the
value will be discarded
– Doesn't fix references to destroyed processes
• These are the folks who used to lazily initialize
their database connections.
37.
38.
39.
40. Independent
• What happens when firing the AT is a contract
with the UI, and must complete?
• Do the simplest possible proxy to the model
• The inverse of the RESTful ContentProvider
cache
43. Good Use Cases
• Serially executed tasks decoupled from other
component lifecycles.
• Off-load UI thread from BroadcastReceiver.
• REST client (ResultReceiver as callback)
IntentService
44. AsyncQueryHandler
• API Level 1
• Asynchronous operations on a
ContentResolver
• Query
• Insert
• Delete
• Update
• Wraps a HandlerThread
45. How it works
AsyncQueryHandler
UI
Thread
BG
Thread
new AsyncQueryHandler();
1
startQuery();
startInsert();
startUpdate();
startDelete();
2
onQueryComplete();
onInsertComplete();
onUpdateComplete();
onDeleteComplete();
3
DB
operations
46. Cons
• No cursor management
• No content observation
• No data retention on configuration changes
• Background thread can’t be forced to quit
AsyncQueryHandler
47. Loader
• API added in Honeycomb
• Available in compatibility package
• Load data in a background thread
• Observes data changes
• Retained on configuration changes
• Connected to the Activity and Fragment lifecycles