• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Efficient Android Threading
 

Efficient Android Threading

on

  • 35,035 views

 

Statistics

Views

Total Views
35,035
Views on SlideShare
32,005
Embed Views
3,030

Actions

Likes
84
Downloads
692
Comments
17

34 Embeds 3,030

http://www.jayway.com 1150
http://de.droidcon.com 549
http://www.blogc.at 407
http://blog.kibotu.net 363
http://moin.neocoin.net 313
https://twitter.com 106
http://www.scoop.it 38
http://houat.inovatel.com 28
http://eventifier.co 24
http://m.themobileprimitives.com 9
http://blog.naver.com 5
https://t.co 5
http://bundlr.com 3
http://themobileprimitives.com 3
http://www.schoox.com 3
http://feeds2.feedburner.com 3
http://www.themobileprimitives.com 2
http://www.hanrss.com 2
http://twitter.com 2
http://www.themobileprimitives.be 1
http://themobileprimitives.be 1
https://www.google.co.il 1
http://es.flavors.me 1
http://java.dzone.com 1
http://www.eventifier.co 1
http://newsblur.com 1
http://www.verious.com 1
http://dev.raulete.com 1
http://plus.url.google.com 1
https://si0.twimg.com 1
https://duckduckgo.com 1
http://fr.flavors.me 1
http://flavors.me 1
http://eventifier.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

110 of 17 previous next Post a comment

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…

110 of 17 previous next

Post Comment
Edit your comment
  • Android app has only UI thread from the startJava threads = Native Linux threads
  • A Java Object goes through one Component lifecycle
  • A Java Object goes through one Component lifecycle
  • Compare different techniques to handle background execution.
  • Future compatibility with more CPUs
  • Future compatibility with more CPUs
  • A Java Object goes through one Component lifecycle
  • A Java Object goes through one Component lifecycle

Efficient Android Threading Efficient Android Threading Presentation Transcript

  • Efficient Android Threading Anders Göransson anders.goransson@jayway.com www.jayway.com www.jayway.com/blog www.oredev.org
  • AgendaOptimize UI thread execution Threads on Android Asynchronous Techniques
  • Threads on Android Android Linux App Process UI Java Threads Native ThreadsBG BG BG
  • Threads on Android Android Scheduling Process level: 1. Foreground 2. Visible Android App 3. Service 4. Background
  • Threads on Android Android Scheduling Foreground Thread Group Process level: App A 1. Foreground > 90% App A 2. Visible Background Thread Group 3. Service App B App B 4. Background < 10%
  • Threads on Android Android Scheduling Foreground Thread Group Process level: App A 1. Foreground > 90% App A 2. Visible Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); Background Thread Group 3. Service App B App B 4. Background < 10%
  • Threads on Android Lifecycles Threads Linux Process UI BG BG BG Java Objects Activity Service Android Components Activity Service Broadcast Content Receiver Provider Broadcast Content Receiver Provider
  • Threads on Android Native Thread Lifecycle Linux Process UI Process BG Application Application Time Start End
  • Threads on Android Example: Activity Lifecycle Activity Component onCreate() onDestroy() onCreate() onDestroy() Activity Object new() GC Activity Object new() GC Time
  • Threads on Android Example: Activity Lifecycle Activity Component onCreate() onDestroy() onCreate() onDestroy() BG StartReference Activity Object new() Activity Object new() Time
  • Threads on Android Background Threads • A thread is a GC root • Implement cancellation policy for your background threads!
  • Use Cases UI BG UI BGThread Thread Thread Thread Get off the Get off the UI thread UI thread early! early! Do long Do long running task running task Report result or update UI
  • GoalWhat technique shall I use for my background thread execution?
  • Asynchronous Techniques• Thread• Executor• HandlerThread• AsyncTask• Service• IntentService• AsyncQueryHandler• Loader
  • Thread• Plain old Java Thread
  • Thread Creation and Start Anonymous Inner Class External Class new Thread(new Runnable() { Thread t = new MyThread(); public void run() { t.start(); // Do long task } }).start(); Implicit reference to outer class
  • Thread Pitfalls • Non-retained threads • Missing cancellation policy • Starting over and over again
  • Thread Good Use Cases • One-shot tasks • Post messages to the UI thread at end.
  • One shot tasksThread
  • Executor• Powerful task execution framework. public interface Executor { public void execute(Runnable r); } Executor Executor Service ThreadPool Executor
  • Executor ExecutorServiceTask submission: executorService.submit(MyTask); executorService.invokeAll(Collection<Tasks>); executorService.invokeAny(Collection<Tasks>);Lifecycle management: executorService.shutdown(); executorService.shutdownNow();Lifecycle observation: executorService.isShutdown(); executorService.isTerminated(); executorService.awaitTermination(); shutdown() Shutting Terminate Running d Down
  • Executor Task / Execution EnvironmentTask:Independent unit of work executed anywhere Runnable Callable run() call() Task manager/observer: Future isDone() isCancelled() cancel() Future future = executorService.submit(Callable);Execution Environment:Technique used to execute the task Executor execute(Runnable)
  • Executor Thread Pools • Executor managing a pool of threads and a work queue. • Reduces overhead of thread creation
  • Executor Thread PoolsFixed Thread Pool Executors.newFixedThreadPool(3)Cached Thread Pool Executors.newCachedThreadPool()Single Thread Pool Executors.newSingleThreadExecutor()Custom Thread Pool new ThreadPoolExecutor(corePoolSize, maximumPoolSize, aliveTime, unit, workQueue)
  • Executor Pitfalls • Lost thread safety when switching from sequential to concurrent execution
  • Executor Good Use Cases • Execute tasks concurrently • Multiple Http requests • Concurrent image processing • Use cases gaining performance from concurrent execution. • Lifecycle management and observation of task execution. • Maximum platform utilization
  • Execution abstraction Repetitive tasks Concurrent execution Sequential execution One shot tasks Execution management Task managementThread Executor
  • HandlerThread• Inherits from Thread and encapsulates a Looper-object.• Thread with a message queue and processing loop.• Handles both Message and Runnable. t = new HandlerThread().start(); t.quit() Running Dead
  • HandlerThread How it works Create and Start Handler 1 Thread Message Queue Add Process 3 2 1 2 t = new HandlerThread("BgThread"); Handler h = new Handler(t.getLooper()) { t.start(); @Override public void handleMessage(Message msg) { //Process message 3 } h.sendEmptyMessage(42); };
  • HandlerThread HandlerRunnable/Message submission: post(Runnable); postDelayed(Runnable); postAtTime(Runnable) postAtFrontOfQueue(Runnable); sendMessage(Message);Lifecycle management: sendMessageDelayed(Message); sendMessageAtTime(Message); sendMessageAtFrontOfQueue(Message);Runnable/Message removal: removeCallbacks(Runnable); removeMessages(Message)
  • HandlerThread Good Use Cases • Keep a thread alive • Sequential execution of messages • Avoid concurrent execution on multiple button clicks • State machine • Detailed control of message processing.
  • Execution abstraction Repetitive tasks Concurrent execution Sequential execution One shot tasks Execution management Task managementThread ExecutorOne shot tasks Concurrent execution “newSingleThreadExecutor”-alike Execute Message and Runnable Better clean up Handler Thread
  • AsyncTask• Wraps Handler/Looper thread communication.• Utilizes the Executor framework.• Callbacks for UI operations and Background operation.
  • AsyncTask How it works UI BG Thread Thread new AsyncTask.execute()1 onPreExecute()2 doInBackground() 3 AsyncTask.cancel() onCancelled() onPostExecute()4b4a
  • AsyncTask How it works public class MyActivity extends Activity { public void onButtonClicked(View v) { new MyAsyncTask().execute("SomeInputString"); } } public class MyAsyncTask extends AsyncTask<String, Void, Integer> { @Override protected void onPreExecute() { } @Override protected Integer doInBackground(String... params) { return null; } @Override protected void onCancelled(Integer result) { } @Override protected void onPostExecute(Integer result) { } }
  • AsyncTask Pitfalls • Application Global Behavior • execute() • Cancellation • Creation and Start
  • AsyncTask > Pitfalls Application Global Behavior Servic e execute() Activity execute() Queue Thread Pool Activity execute() Receiver execute() * execute()
  • AsyncTask > Pitfalls execute() Execution behavior has changed over time< Donut: execute()< Honeycomb: execute()>= Honeycomb: execute()executeOnExecutor(Executor)
  • AsyncTask > Pitfalls execute() “So, if I call AsyncTask.execute on Honeycomb and later my tasks will run sequentially, right?” “Right?!?” “Eh, it depends…” <uses-sdk android:targetSdkVersion="12" /> <uses-sdk android:targetSdkVersion="13" />
  • AsyncTask > Pitfalls Cancellation • Cancel AsyncTask when component finishes lifecycle. • Avoid UI updates on unavailable component. • cancel(true) == cancel(false) + interrupt() • Implement cancellation policy.
  • AsyncTask > Pitfalls Creation and Start • at = new AsyncTask() • The first creation decides callback thread • onPostExecute() • onPublishProgress() • onCancelled() • at.execute() • Callback thread of onPreExecute()
  • Execution abstraction Repetitive tasks Concurrent execution Sequential execution One shot tasks Execution management Task management Thread Executor Concurrent execution Task control “newSingleThreadExecutor”AsyncTask<Void, Void, Void> Execute Message and RunnableAsyncTask.execute(Runnable) Better clean up Handler Thread Subsequent execution AsyncTask One task with UI callback
  • Service• Background execution on the UI thread• Decouple background threads with other lifecycles, typically the Activity lifecycle.
  • Service Example: Lifecycle Decoupling Activity Component onCreate() onDestroy() onCreate() onDestroy() Service Component onCreate() onDestroy() BG Time
  • Service Good Use Cases • Tasks executed independently of user interaction. • Tasks executing over several Activity lifecycles or configuration changes.
  • Service Pitfalls • Hidden AsyncTask.execute() with serial execution.
  • Execution abstraction Repetitive tasks Concurrent execution Sequential execution One shot tasks Execution management Task management Thread Executor Concurrent execution Task control “newSingleThreadExecutor”AsyncTask<Void, Void, Void> Execute Message and RunnableAsyncTask.execute(Runnable) Better clean up Handler Thread Subsequent execution AsyncTask Service Activity lifecycle One task with UI callback independent tasks
  • IntentService• Service with a worker thread.• On demand intents. public class MyIntentService extends IntentService { @Override protected void onHandleIntent(Intent intent) { } }
  • IntentService Lifecycle IntentService Component onCreate() onDestroy() BG Intent 1 Intent 2 Intent 3 Intent n stopSelf() Time
  • IntentService Good Use Cases • Serially executed tasks decoupled from other component lifecycles. • Off-load UI thread from BroadcastReceiver. • REST client (ResultReceiver as callback)
  • Execution abstraction Repetitive tasks Concurrent execution Sequential execution One shot tasks Execution management Task management Thread Executor Concurrent execution Task control “newSingleThreadExecutor”AsyncTask<Void, Void, Void> Execute Message and RunnableAsyncTask.execute(Runnable) Better clean up Handler Thread Subsequent execution “On “Continuous” demand” Intent AsyncTask Service Service Activity lifecycle One task with UI callback independent tasks
  • AsyncQueryHandler• API Level 1• Asynchronous operations on a ContentResolver • Query • Insert • Delete • Update• Wraps a HandlerThread
  • AsyncQueryHandler How it works UI BG Thread Thread new AsyncQueryHandler(); 1 startQuery(); startInsert(); startUpdate(); startDelete(); 2 DB operations 3 onQueryComplete(); onInsertComplete(); onUpdateComplete(); onDeleteComplete();
  • AsyncQueryHandler Cons • No cursor management • No content observation • No data retention on configuration changes • Background thread can’t be forced to quit
  • Execution abstraction Repetitive tasks Concurrent execution Sequential execution Asynchronous CRUD One shot tasks Execution management Task management Async Thread Executor Query Handler Concurrent execution Task control “newSingleThreadExecutor”AsyncTask<Void, Void, Void> Execute Message and RunnableAsyncTask.execute(Runnable) Better clean up Handler Thread Subsequent execution “On “Continuous” demand” Intent AsyncTask Service Service Activity lifecycle One task with UI callback independent tasks
  • 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
  • Loader Basics 3 Lifecycle management 4 Retained on configuration change Loader 1 Load data in BG 2 Observe data Data Source
  • Loader Data Sources Custom Cursor Loader Loader Any Data Content Source Provider
  • Loader How It Works public class AndroidLoaderActivity extends ListActivity implements LoaderCallbacks<Cursor>{ SimpleCursorAdapter mAdapter; public void onCreate(Bundle savedInstanceState) { getLoaderManager().initLoader(0, null, this); } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { return new CursorLoader(..., CONTENT_URI, ...); } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor c) { mAdapter.swapCursor(c); } @Override public void onLoaderReset(Loader<Cursor> arg0) { mAdapter.swapCursor(null); } }
  • Loader Pitfalls • Data loading will stop when Activity/Fragment is destroyed • Http requests not finished
  • Loader Good Use Cases • Loading data from Content Providers.
  • Execution abstraction Repetitive tasks Concurrent execution Sequential execution Asynchronous CRUD One shot tasks Execution management Task management Async Thread Executor Query Handler Concurrent execution Task control Full CRUD “newSingleThreadExecutor”AsyncTask<Void, Void, Void> Execute Message and Runnable Load data from CPAsyncTask.execute(Runnable) Better clean up Data observing Handler Custom Thread Loader Subsequent execution Loader “On “Continuous” demand” Intent AsyncTask Service Service Activity lifecycle One task with UI callback independent tasks
  • Thank you for listening! Questions?