Is Android’s AsyncTask executing tasks serially or concurrently?

This post focuses on one method only and in the case of AsyncTask.execute() it’s certainly motivated. The execution behavior is often misunderstood and it’s not easy to know whether tasks will be executed serially or concurrently.

The Problem

AsyncTask is the most commonly used background execution technique on Android and for most use cases we want to know how execution of multiple tasks is performed. If the tasks utilize common resources, e.g. the file system, we probably want to simplify the handling by executing them serially. On the other hand, if we have multiple independent tasks we want to improve throughput by executing them concurrently. So, how should we know if the following code executes our tasks as expected:

Platform Level Differences

The main cause of confusion is that the behavior has changed over time and at first the changes were considered to be an internal implementation detail and not documented. The documentation improved in Honeycomb but still omits important information.

Before Donut:
The first versions of AsyncTask.execute() let all tasks execute serially., Hence, before a task can execute, all the previous tasks have to be finished. The problem with this approach is that tasks delayed each other and the throughput could be bad when many long running tasks were executed with the AsyncTask.

From Donut to Gingerbread:
To improve throughput the behavior of the AsyncTask was changed so that each task was executed on a separate thread. The problem with this was that many users had relied on the sequential behavior and now suddenly started to encounter concurrency issues. Even the Android platform suffered from this internally.

Honeycomb and onwards:
Execution was switched back to the sequential implementation and another method executeOnExecutor(Executor) was added if parallel execution was needed.

In conclusion this means that if our application cares about whether the tasks are executed serial or parallel we need to check in runtime what platform version we are executing on and define the execution accordingly.

The targetSdkVersion Caveat

With the knowledge described above of how AsyncTask.execute() has changed over time it seems safe to say that when I execute a task on Honeycomb or later platforms all tasks will be executed serially. Unfortunately, also the android:targetSdkVersion attribute set in the AndroidManifest.xml makes a difference.

The targetSdkVersion refers to the API level that your application targets. If not defined it’s set to minSdkVersion. Let us see why this has an impact for our use case by looking at two code extracts from AsyncTask.java and ActivityThread.java in Honeycomb:

The two code snippets above shows that as of Honeycomb the platform can change the default behavior of the execution environment in the AsyncTask in runtime and this is exactly what ActivityThread is doing. If we have set the targetSdkVersion lower than Honeycomb, AsyncTask.execute() will still execute tasks in parallel although we would expect serial execution. In clarity:

AsyncTask.execute() will execute tasks in parallel on Honeycomb and onwards.

AsyncTask.execute() will execute tasks serially on Honeycomb and onwards.

Unfortunately nothing concerning the impact of targetSdkVersion is mentioned in the documentation, but now you know 🙂

In practice, if we would like to execute all our tasks concurrently when targetSdkVersion is 13 or higher we need to do a runtime check to decide how to execute. For instance by a wrapper class (Pre-Donut not included):

If we on the other hand would like to execute tasks serially on all platforms after Donut the AsyncTask is unfortunately not to recommend, since it doesn’t have any serial execution behaviour between the Donut and Honeycomb versions. Instead the application should utilize a HandlerThread or Executors.newSingleThreadExecutor().

Reference : http://www.jayway.com/2012/11/28/is-androids-asynctask-executing-tasks-serially-or-concurrently/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s