Multi-threading Android Apps for Multi-core Processors – Part 1 of 2

Friday 3/15/13 03:52pm
|
Posted By Tushar Gupta
  • Up0
  • Down0

As an application developer, why should you care about additional cores in application processors? Some time back, my colleague Liat Ben-Zur asked whether anyone is really taking advantage of multiple cores, and I want to pick up that theme again.

Multi-core application processors, including today’s Qualcomm® Snapdragon™ processors, power most of the Android smartphones available today. Support for multiple cores in Android was introduced in Honeycomb. Some apps see automatic performance gains on devices with multi-core processors, but you can also write code specifically to take advantage of multiple cores for additional optimization. In this post and the next one, I’ll walk you through three parallelization techniques for Java-based Android applications.

First, some basics.

Can my single-threaded application benefit from multiple cores? How?

Even a single-threaded application can benefit from parallel processing on different cores. For example, if your application uses a media server, then the media processing and your UI rendering application logic can run on different cores at the same time. Also, the garbage collector can run on a different core. 

How can I write code that takes advantage of multiple cores?

To realize the maximum potential of the available processing power on multi-core devices, write your application with concurrency in mind. The application should be designed so that tasks which can be executed in parallel are set up to run on separate threads.

The UI Thread

In Android, the main thread is the same as the UI thread. This thread is responsible for handling all the UI events.

When you’re writing multi-threaded applications in Android, keep these things in mind about the UI thread (or main thread):

  • Only the main thread should update the UI. All other threads in the application should return data back to the main thread to update the UI.
  • There is no single point of entry in an Android application. An Android application can be entered from an Activity, Service or a Broadcast Receiver, all of which run on the UI thread.
  • Very important – The UI thread should not perform tasks that take longer than a few seconds, or else you run the risk of a sluggish user experience in your app. Read Android documentation about Keeping Your App Responsive to learn more.

As a rule of thumb, whenever your application needs to perform a longer task(s) from the UI thread, then it should parallelize using one of the parallelization techniques provided by Android: Java threads, AsyncTask or IntentService.

Parallelization technique: Java threads

The standard ways of creating threads in Java are also available in Android: extending the Thread class or implement the Runnable interface. However, if you want to pass messages to and from a thread, you need to implement message queues using android.os.Message, android.os.Handler, android.os.Looper, etc.

Also, if your application involves creating multiple threads then you might have to take care of multi-threading concurrency issues like race conditions, deadlocks and starvation. To learn more about Java threads and message passing between threads, read through the Java tutorial, Defining and Starting a Thread.

When should I use Java threads for parallelization?

When you want to parallelize tasks running longer than a few seconds, you should use Java threads from the UI thread. Android provides other, easier ways to parallelize, like AsyncTask and IntentService, so for most simple applications you may not need Java threads.

Parallelization technique: AsyncTask

AsyncTask provides the functionality to run short tasks (a couple of seconds long) in the background from the UI thread using a method called doInBackground(). You do not need to implement any message passing to and from the UI thread. 
An AsyncTask uses 3 types of data:

  • Params - parameters passed to the background method as inputs.
  • Progress - data passed to the UI thread for updating progress.
  • Result - data returned from the background method upon completion.

You can implement an AsyncTask using the following steps, found in Android’s AsyncTask documentation:

  • Extend the AsyncTask class.
  • Implement the following methods:
    • onPreExecute() - performs setup like showing a progress dialog before executing the task. This method is invoked on the UI thread.
    • doInBackground(Params...) - executes all of the code that you want to run in the background and sends updates to onProgressUpdate() and the result to onPostExecute(Result). It is invoked on a pool of background threads.
    • onProgressUpdate() - invoked when publishProgress() is called from the doInBackground() method. It is invoked on the UI thread. 
    • onPostExecute() - receives the return value from doInBackground(). It is invoked on the UI thread.
    • onCancelled() - invoked when cancel() is called. Invoked on the UI thread.
  • Create an instance of your extended AsyncTask class on the UI thread.
  • Call the execute() method.

Rules

  • Your extended AsyncTask class is executed by calling the execute() method, which runs on the UI thread and spawns a separate background thread to execute your task.
  • The order of execution of AsyncTask methods is: onPreExecute() -> doInBackground(Params…) -> onProgressUpdate() -> onPostExecute() or onCancelled().
  • None of the above methods can be invoked directly.
  • One instance of an AsyncTask can only be executed once. Execute should be called using multiple instances if you want to run multiple tasks.
  • An AsyncTask must be created on the UI thread.

When should I use an AsyncTask for parallelization?

Use an AsyncTask whenever you have a short background task which needs communication with the UI thread. AsyncTask is appropriate for short tasks only because it creates and manages threads for you, and you don't want to tie up resources with thread(s) that you did not create. Use Java threads and handlers from inside a service for longer-running (more than a few seconds) tasks. See the Android documentation on AsyncTask for more information.

Your turn

Ready to try Java threads and AsyncTask for yourself? Try them on background tasks in your own code and send me any questions in the comments below.

In my next post, I'll walk you through IntentService, another way to parallelization technique for Android.

Comments

Re: Multi-threading Android Apps for Multi-core Processors –...

HI 

IM A BEGINNER USING ASYNCTASK

IM MAKINK A LOOP ON EXECUTE I DONT HOW I WORKS DIDNT FIND ANY DETAILED DOCUMENTATION OF HOW IT WORKS IM HAVING AN ERROR Skipped 43 frames!  The application may be doing too mufor(i = 0 ; i < wpObjects.size() ; i++)

{
  new        getParseData().executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, wpObjects.get(i).urls);
}
there are 3 xml urls to each object which asynctask is looping and passing arraylist of headlines from each url
and then in the postexecute i set the costume adapter i need an explanetion of how the execute behaves in a loop there is not much information
or maybe parsing is to much long processing for asynctask
thank you
ravit
 

ch work on its main thread.THE LOOP IS LIKE THIS:

Re: Multi-threading Android Apps for Multi-core Processors –...

We would like to implement speech processing algorithms on SnapDragon based Android Smart phone. Speech processing algorithm will process PCM speech coming from microphone differently than speech coming from CODEC during telephonic conversation on 3G network. As soon as smart phone rings, we would like CODEC output to come to our algorithm before going to earphone. Our algorithms will process CODEC output and send the processed speech to earphone.

Could you please tell us if such a thing is possible in Snapdragon and Android based smart phone? It this is possible to do, please tell us how. Is there a reference circuit available of a smart mobile phone based on Snapdragon? Thanks.