Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1099426
  • 博文数量: 276
  • 博客积分: 8317
  • 博客等级: 少将
  • 技术积分: 2329
  • 用 户 组: 普通用户
  • 注册时间: 2006-09-12 08:17
个人简介

http://ads.buzzcity.net/adpage.php?partnerid=40096

文章分类

全部博文(276)

文章存档

2013年(1)

2012年(38)

2011年(102)

2010年(85)

2009年(45)

2008年(5)

分类: 嵌入式

2012-12-26 16:44:40

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:

  1.  
  2. new AsyncTask1().execute();
  3. new AsyncTask2().execute();
  4.  
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.

  1.  
  2. android:minSdkVersion="integer"
  3. android:targetSdkVersion="integer"
  4. android:maxSdkVersion="integer" />
  5.  

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:

  1.  
  2. // AsyncTask.java in Honeycomb
  3. /** @hide */
  4. public static void setDefaultExecutor(Executor exec) {
  5. sDefaultExecutor = exec;
  6. }
  7.  
  1.  
  2. // ActivityThread.java in Honeycomb
  3. if (data.appInfo.targetSdkVersion <=
  4. android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
  5. AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
  6. }
  7.  

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:

  1.  
  2. android:targetSdkVersion="12" />
  3.  

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

  1.  
  2. android:targetSdkVersion="13"/>
  3.  

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):

  1.  
  2. public class ConcurrentAsyncTask {
  3. public static void execute(AsyncTask as) {
  4. if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB_MR1) {
  5. as.execute();
  6. } else {
  7. as.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
  8. }
  9. }
  10. }
  11.  

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().

/Anders

阅读(2380) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~