SlideShare a Scribd company logo
1 of 63
Efficient Android Threading
           Anders Göransson

      anders.goransson@jayway.com

            www.jayway.com
          www.jayway.com/blog
            www.oredev.org
Agenda

Optimize UI thread execution

       Threads on Android


     Asynchronous Techniques
Threads on Android


                         Android                     Linux
                          App                       Process

         UI


     Java Threads                  Native Threads


BG       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
                               Start
Reference


      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                    BG
Thread                 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
Goal


What 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 tasks


Thread
Executor
• Powerful task execution framework.
         public interface Executor {

                        public void execute(Runnable r);

         }




             Executor


             Executor
             Service


         ThreadPool
          Executor
Executor

                                       ExecutorService
Task 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 Environment
Task:
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 Pools
Fixed 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 management

Thread                                       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

                                       Handler
Runnable/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 management

Thread                                                          Executor

One 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()
4b
4a
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 Runnable
AsyncTask.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 Runnable
AsyncTask.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 Runnable
AsyncTask.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 Runnable
AsyncTask.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 CP
AsyncTask.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?

More Related Content

What's hot

Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Javaparag
 
Inter thread communication
Inter thread communicationInter thread communication
Inter thread communicationsubash andey
 
Cours #9 L'Internet des objets
Cours #9 L'Internet des objetsCours #9 L'Internet des objets
Cours #9 L'Internet des objetsAlexandre Moussier
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentationguest11106b
 
Java applets
Java appletsJava applets
Java appletsPihu Goel
 
Créer des applications Java avec MongoDB
Créer des applications Java avec MongoDBCréer des applications Java avec MongoDB
Créer des applications Java avec MongoDBMongoDB
 
Java Multiple Choice Questions and Answers
Java Multiple Choice Questions and AnswersJava Multiple Choice Questions and Answers
Java Multiple Choice Questions and AnswersJava Projects
 
Implementing DDD with C#
Implementing DDD with C#Implementing DDD with C#
Implementing DDD with C#Pascal Laurin
 
Résumer sur les fichier et les enregistrement
Résumer sur les fichier et les enregistrementRésumer sur les fichier et les enregistrement
Résumer sur les fichier et les enregistrementborhen boukthir
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced JavascriptAdieu
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaRaghu nath
 

What's hot (20)

Initiation au html
Initiation au htmlInitiation au html
Initiation au html
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
Inter thread communication
Inter thread communicationInter thread communication
Inter thread communication
 
Cours #9 L'Internet des objets
Cours #9 L'Internet des objetsCours #9 L'Internet des objets
Cours #9 L'Internet des objets
 
sets and maps
 sets and maps sets and maps
sets and maps
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
 
Android Thread
Android ThreadAndroid Thread
Android Thread
 
Generics
GenericsGenerics
Generics
 
Java applets
Java appletsJava applets
Java applets
 
Typescript ppt
Typescript pptTypescript ppt
Typescript ppt
 
Créer des applications Java avec MongoDB
Créer des applications Java avec MongoDBCréer des applications Java avec MongoDB
Créer des applications Java avec MongoDB
 
Java Multiple Choice Questions and Answers
Java Multiple Choice Questions and AnswersJava Multiple Choice Questions and Answers
Java Multiple Choice Questions and Answers
 
Implementing DDD with C#
Implementing DDD with C#Implementing DDD with C#
Implementing DDD with C#
 
Routing in 6lowpan (in French)
Routing in 6lowpan (in French) Routing in 6lowpan (in French)
Routing in 6lowpan (in French)
 
Résumer sur les fichier et les enregistrement
Résumer sur les fichier et les enregistrementRésumer sur les fichier et les enregistrement
Résumer sur les fichier et les enregistrement
 
jQuery
jQueryjQuery
jQuery
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Reactjs
Reactjs Reactjs
Reactjs
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Machine-learning-FR.pdf
Machine-learning-FR.pdfMachine-learning-FR.pdf
Machine-learning-FR.pdf
 

Similar to Efficient Android Threading Techniques

Eclispe daytoulouse combining the power of eclipse with android_fr_1024_768_s...
Eclispe daytoulouse combining the power of eclipse with android_fr_1024_768_s...Eclispe daytoulouse combining the power of eclipse with android_fr_1024_768_s...
Eclispe daytoulouse combining the power of eclipse with android_fr_1024_768_s...Mathias Seguy
 
Behavior Driven Development by Example
Behavior Driven Development by ExampleBehavior Driven Development by Example
Behavior Driven Development by ExampleNalin Goonawardana
 
Inside the android_application_framework
Inside the android_application_frameworkInside the android_application_framework
Inside the android_application_frameworksurendray
 
Griffon for the Enterprise
Griffon for the EnterpriseGriffon for the Enterprise
Griffon for the EnterpriseJames Williams
 
J1 2015 "Debugging Java Apps in Containers: No Heavy Welding Gear Required"
J1 2015 "Debugging Java Apps in Containers: No Heavy Welding Gear Required"J1 2015 "Debugging Java Apps in Containers: No Heavy Welding Gear Required"
J1 2015 "Debugging Java Apps in Containers: No Heavy Welding Gear Required"Daniel Bryant
 
Kandroid for nhn_deview_20131013_v5_final
Kandroid for nhn_deview_20131013_v5_finalKandroid for nhn_deview_20131013_v5_final
Kandroid for nhn_deview_20131013_v5_finalNAVER D2
 
soft-shake.ch - Windows Phone 7 „Mango“ – what’s new for Developers?
soft-shake.ch - Windows Phone 7 „Mango“ – what’s new for Developers?soft-shake.ch - Windows Phone 7 „Mango“ – what’s new for Developers?
soft-shake.ch - Windows Phone 7 „Mango“ – what’s new for Developers?soft-shake.ch
 
Android Jumpstart Jfokus
Android Jumpstart JfokusAndroid Jumpstart Jfokus
Android Jumpstart JfokusLars Vogel
 
Android 101 - Introduction to Android Development
Android 101 - Introduction to Android DevelopmentAndroid 101 - Introduction to Android Development
Android 101 - Introduction to Android DevelopmentAndy Scherzinger
 
Spring batch showCase
Spring batch showCaseSpring batch showCase
Spring batch showCasetaher abdo
 
Study of solution development methodology for small size projects.
Study of solution development methodology for small size projects.Study of solution development methodology for small size projects.
Study of solution development methodology for small size projects.Joon ho Park
 
DSC NTUE Info Session
DSC NTUE Info SessionDSC NTUE Info Session
DSC NTUE Info Sessionssusera8eac9
 

Similar to Efficient Android Threading Techniques (20)

Android concurrency
Android concurrencyAndroid concurrency
Android concurrency
 
Guides To Analyzing WebKit Performance
Guides To Analyzing WebKit PerformanceGuides To Analyzing WebKit Performance
Guides To Analyzing WebKit Performance
 
Applied Computer Science Concepts in Android
Applied Computer Science Concepts in AndroidApplied Computer Science Concepts in Android
Applied Computer Science Concepts in Android
 
Eclispe daytoulouse combining the power of eclipse with android_fr_1024_768_s...
Eclispe daytoulouse combining the power of eclipse with android_fr_1024_768_s...Eclispe daytoulouse combining the power of eclipse with android_fr_1024_768_s...
Eclispe daytoulouse combining the power of eclipse with android_fr_1024_768_s...
 
Behavior Driven Development by Example
Behavior Driven Development by ExampleBehavior Driven Development by Example
Behavior Driven Development by Example
 
Android101
Android101Android101
Android101
 
Netflix conductor
Netflix conductorNetflix conductor
Netflix conductor
 
Inside the android_application_framework
Inside the android_application_frameworkInside the android_application_framework
Inside the android_application_framework
 
Mono for android
Mono for androidMono for android
Mono for android
 
Griffon for the Enterprise
Griffon for the EnterpriseGriffon for the Enterprise
Griffon for the Enterprise
 
J1 2015 "Debugging Java Apps in Containers: No Heavy Welding Gear Required"
J1 2015 "Debugging Java Apps in Containers: No Heavy Welding Gear Required"J1 2015 "Debugging Java Apps in Containers: No Heavy Welding Gear Required"
J1 2015 "Debugging Java Apps in Containers: No Heavy Welding Gear Required"
 
Kandroid for nhn_deview_20131013_v5_final
Kandroid for nhn_deview_20131013_v5_finalKandroid for nhn_deview_20131013_v5_final
Kandroid for nhn_deview_20131013_v5_final
 
soft-shake.ch - Windows Phone 7 „Mango“ – what’s new for Developers?
soft-shake.ch - Windows Phone 7 „Mango“ – what’s new for Developers?soft-shake.ch - Windows Phone 7 „Mango“ – what’s new for Developers?
soft-shake.ch - Windows Phone 7 „Mango“ – what’s new for Developers?
 
Node.JS briefly introduced
Node.JS briefly introducedNode.JS briefly introduced
Node.JS briefly introduced
 
Android Jumpstart Jfokus
Android Jumpstart JfokusAndroid Jumpstart Jfokus
Android Jumpstart Jfokus
 
Android 101 - Introduction to Android Development
Android 101 - Introduction to Android DevelopmentAndroid 101 - Introduction to Android Development
Android 101 - Introduction to Android Development
 
Spring batch showCase
Spring batch showCaseSpring batch showCase
Spring batch showCase
 
Improve Android System Component Performance
Improve Android System Component PerformanceImprove Android System Component Performance
Improve Android System Component Performance
 
Study of solution development methodology for small size projects.
Study of solution development methodology for small size projects.Study of solution development methodology for small size projects.
Study of solution development methodology for small size projects.
 
DSC NTUE Info Session
DSC NTUE Info SessionDSC NTUE Info Session
DSC NTUE Info Session
 

Recently uploaded

My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 

Recently uploaded (20)

My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 

Efficient Android Threading Techniques

  • 1. Efficient Android Threading Anders Göransson anders.goransson@jayway.com www.jayway.com www.jayway.com/blog www.oredev.org
  • 2. Agenda Optimize UI thread execution Threads on Android Asynchronous Techniques
  • 3. Threads on Android Android Linux App Process UI Java Threads Native Threads BG BG BG
  • 4. Threads on Android Android Scheduling Process level: 1. Foreground 2. Visible Android App 3. Service 4. Background
  • 5. 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%
  • 6. 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%
  • 7. 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
  • 8. Threads on Android Native Thread Lifecycle Linux Process UI Process BG Application Application Time Start End
  • 9. Threads on Android Example: Activity Lifecycle Activity Component onCreate() onDestroy() onCreate() onDestroy() Activity Object new() GC Activity Object new() GC Time
  • 10. Threads on Android Example: Activity Lifecycle Activity Component onCreate() onDestroy() onCreate() onDestroy() BG Start Reference Activity Object new() Activity Object new() Time
  • 11. Threads on Android Background Threads • A thread is a GC root • Implement cancellation policy for your background threads!
  • 12. Use Cases UI BG UI BG Thread 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
  • 13. Goal What technique shall I use for my background thread execution?
  • 14. Asynchronous Techniques • Thread • Executor • HandlerThread • AsyncTask • Service • IntentService • AsyncQueryHandler • Loader
  • 15. Thread • Plain old Java Thread
  • 16. 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
  • 17. Thread Pitfalls • Non-retained threads • Missing cancellation policy • Starting over and over again
  • 18. Thread Good Use Cases • One-shot tasks • Post messages to the UI thread at end.
  • 20. Executor • Powerful task execution framework. public interface Executor { public void execute(Runnable r); } Executor Executor Service ThreadPool Executor
  • 21. Executor ExecutorService Task 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
  • 22. Executor Task / Execution Environment Task: 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)
  • 23. Executor Thread Pools • Executor managing a pool of threads and a work queue. • Reduces overhead of thread creation
  • 24. Executor Thread Pools Fixed 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)
  • 25. Executor Pitfalls • Lost thread safety when switching from sequential to concurrent execution
  • 26. 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
  • 27. Execution abstraction Repetitive tasks Concurrent execution Sequential execution One shot tasks Execution management Task management Thread Executor
  • 28. 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
  • 29. 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); };
  • 30. HandlerThread Handler Runnable/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)
  • 31. 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.
  • 32. Execution abstraction Repetitive tasks Concurrent execution Sequential execution One shot tasks Execution management Task management Thread Executor One shot tasks Concurrent execution “newSingleThreadExecutor”-alike Execute Message and Runnable Better clean up Handler Thread
  • 33. AsyncTask • Wraps Handler/Looper thread communication. • Utilizes the Executor framework. • Callbacks for UI operations and Background operation.
  • 34. AsyncTask How it works UI BG Thread Thread new AsyncTask.execute() 1 onPreExecute() 2 doInBackground() 3 AsyncTask.cancel() onCancelled() onPostExecute() 4b 4a
  • 35. 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) { } }
  • 36. AsyncTask Pitfalls • Application Global Behavior • execute() • Cancellation • Creation and Start
  • 37. AsyncTask > Pitfalls Application Global Behavior Servic e execute() Activity execute() Queue Thread Pool Activity execute() Receiver execute() * execute()
  • 38. AsyncTask > Pitfalls execute() Execution behavior has changed over time < Donut: execute() < Honeycomb: execute() >= Honeycomb: execute() executeOnExecutor(Executor)
  • 39. 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" />
  • 40. AsyncTask > Pitfalls Cancellation • Cancel AsyncTask when component finishes lifecycle. • Avoid UI updates on unavailable component. • cancel(true) == cancel(false) + interrupt() • Implement cancellation policy.
  • 41. AsyncTask > Pitfalls Creation and Start • at = new AsyncTask() • The first creation decides callback thread • onPostExecute() • onPublishProgress() • onCancelled() • at.execute() • Callback thread of onPreExecute()
  • 42. 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 Runnable AsyncTask.execute(Runnable) Better clean up Handler Thread Subsequent execution AsyncTask One task with UI callback
  • 43. Service • Background execution on the UI thread • Decouple background threads with other lifecycles, typically the Activity lifecycle.
  • 44. Service Example: Lifecycle Decoupling Activity Component onCreate() onDestroy() onCreate() onDestroy() Service Component onCreate() onDestroy() BG Time
  • 45. Service Good Use Cases • Tasks executed independently of user interaction. • Tasks executing over several Activity lifecycles or configuration changes.
  • 46. Service Pitfalls • Hidden AsyncTask.execute() with serial execution.
  • 47. 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 Runnable AsyncTask.execute(Runnable) Better clean up Handler Thread Subsequent execution AsyncTask Service Activity lifecycle One task with UI callback independent tasks
  • 48. IntentService • Service with a worker thread. • On demand intents. public class MyIntentService extends IntentService { @Override protected void onHandleIntent(Intent intent) { } }
  • 49. IntentService Lifecycle IntentService Component onCreate() onDestroy() BG Intent 1 Intent 2 Intent 3 Intent n stopSelf() Time
  • 50. IntentService Good Use Cases • Serially executed tasks decoupled from other component lifecycles. • Off-load UI thread from BroadcastReceiver. • REST client (ResultReceiver as callback)
  • 51. 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 Runnable AsyncTask.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
  • 52. AsyncQueryHandler • API Level 1 • Asynchronous operations on a ContentResolver • Query • Insert • Delete • Update • Wraps a HandlerThread
  • 53. AsyncQueryHandler How it works UI BG Thread Thread new AsyncQueryHandler(); 1 startQuery(); startInsert(); startUpdate(); startDelete(); 2 DB operations 3 onQueryComplete(); onInsertComplete(); onUpdateComplete(); onDeleteComplete();
  • 54. AsyncQueryHandler Cons • No cursor management • No content observation • No data retention on configuration changes • Background thread can’t be forced to quit
  • 55. 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 Runnable AsyncTask.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
  • 56. 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
  • 57. Loader Basics 3 Lifecycle management 4 Retained on configuration change Loader 1 Load data in BG 2 Observe data Data Source
  • 58. Loader Data Sources Custom Cursor Loader Loader Any Data Content Source Provider
  • 59. 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); } }
  • 60. Loader Pitfalls • Data loading will stop when Activity/Fragment is destroyed • Http requests not finished
  • 61. Loader Good Use Cases • Loading data from Content Providers.
  • 62. 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 CP AsyncTask.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
  • 63. Thank you for listening! Questions?

Editor's Notes

  1. Android app has only UI thread from the startJava threads = Native Linux threads
  2. A Java Object goes through one Component lifecycle
  3. A Java Object goes through one Component lifecycle
  4. Compare different techniques to handle background execution.
  5. Future compatibility with more CPUs
  6. Future compatibility with more CPUs
  7. A Java Object goes through one Component lifecycle
  8. A Java Object goes through one Component lifecycle