Part 2: Multi-threading-android-apps-multi-core...

In my last post, I offered some guidelines on writing code to take advantage of multi-core application processors like today’s Qualcomm® Snapdragon™ processors. I mentioned that Java threads are one way of parallelizing tasks, then described AsyncTask, an easier Android construct.

In this post, I’ll describe IntentService, another Android construct that’s easier to implement than Java threads.

Parallelization technique: IntentService

Android provides 'Service' to execute an operation that requires no user interaction. Service runs in the application’s main thread. You can use an IntentService to execute a long-running task in a separate thread without having to handle creation and management of threads.

In your code, you extend the IntentService class and implement the onHandleIntent() method. When the IntentService is triggered using an intent, it spawns a new worker thread and the method onHandleIntent() is called on this thread.

Here is a code snippet showing how to implement an IntentService: 

class MyIntentService extends IntentService {
    private static final String TAG = "MyIntentService";
    public MyIntentService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
    //Code to run your task
    } 
}


Rules

  1. Code running in onHandleIntent() does not block the UI thread.
  2. If multiple intents are received, then they do NOT execute in parallel. They are executed sequentially in the order in which they were received. 
  3. After your task has finished executing and there are no intents remaining to be processed, then the IntentService is automatically stopped.

Results can be broadcast back to the application, if required.

When should I use an IntentService for parallelization?

IntentService is designed to use:

  • whenever there is a long task which needs to be performed multiple times, and which does not require user interaction. IntentService provides a very easy way to manage queues for tasks such as accessing a content provider, network operations and processor-intensive math operations.
  • whenever you have a task that you want to trigger in the background without having to launch the UI, and which does not require an always-on Service. For example, you could register a PendingIntent for your IntentService and hence start the IntentService whenever that PendingIntent is fired.

Summary

So, based on the task type, this table summarizes parallelization techniques: 

Task Type Parallelization Technique
Short task in the UI thread requiring one thread AsyncTask
Short task in the UI thread requiring a thread pool AsyncTask
Longer task in the UI thread that requires passing messages back and forth Java Threads, Handlers and Loopers
Longer task that does not require user interaction and needs only one worker thread IntentService
Longer task that does not require user interaction and needs a thread pool Java Threads, Handlers and Loopers inside a Service

Your turn

How are you accomplishing parallelization in your own code? Give Java threads, AsyncTask and IntentService a try and let me know what difference you see. Leave any questions or suggestions you have in the comments below.

你可能感兴趣的:(android,muti-thread)