在Android 1.6之前,AsyncTask处理任务时是采用串行方式,Android 1.6时,AsyncTask处理任务时是在线程池中并行处理任务。但在Android 3.0开始,AsyncTask又开始采用串行方式处理任务。
我们稍加改造一下上面的示例,在构造方法中传入一个taskId,然后在关键log中输出这个taskId,以便于区分:
private int taskId; public MyAsyncTask(int taskId) { this.taskId = taskId; } /** * 在异步任务执行之前调用 * 执行在主线程中 */ @Override protected void onPreExecute() { super.onPreExecute(); Log.i(TAG, "taskId= " + taskId + ": onPreExecute: "); } // 其他方法类比onPreExecute方法中的log增加taskId,就不贴出了在Activity中初始化两个AsyncTask对象,并调用excute执行任务:
public class AsyncTaskActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_async_task); MyAsyncTask task1 = new MyAsyncTask(1); MyAsyncTask task2 = new MyAsyncTask(2); task1.execute(); task2.execute(); } }log如下:
.../cn.codingblock.thread I/MyAsyncTask: taskId= 1: onPreExecute: .../cn.codingblock.thread I/MyAsyncTask: taskId= 2: onPreExecute: .../cn.codingblock.thread I/MyAsyncTask: taskId= 1: doInBackground: .../cn.codingblock.thread I/MyAsyncTask: taskId= 1: onProgressUpdate: 执行进度:0% .../cn.codingblock.thread I/MyAsyncTask: taskId= 1: onProgressUpdate: 执行进度:10% .../cn.codingblock.thread I/MyAsyncTask: taskId= 1: onProgressUpdate: 执行进度:20% .../cn.codingblock.thread I/MyAsyncTask: taskId= 1: onProgressUpdate: 执行进度:30% .../cn.codingblock.thread I/MyAsyncTask: taskId= 1: onProgressUpdate: 执行进度:40% .../cn.codingblock.thread I/MyAsyncTask: taskId= 1: onProgressUpdate: 执行进度:50% .../cn.codingblock.thread I/MyAsyncTask: taskId= 1: onProgressUpdate: 执行进度:60% .../cn.codingblock.thread I/MyAsyncTask: taskId= 1: onProgressUpdate: 执行进度:70% .../cn.codingblock.thread I/MyAsyncTask: taskId= 1: onProgressUpdate: 执行进度:80% .../cn.codingblock.thread I/MyAsyncTask: taskId= 1: onProgressUpdate: 执行进度:90% .../cn.codingblock.thread I/MyAsyncTask: taskId= 1: onPostExecute: result=taskId= 1: 执行结果:100.0% .../cn.codingblock.thread I/MyAsyncTask: taskId= 2: doInBackground: .../cn.codingblock.thread I/MyAsyncTask: taskId= 2: onProgressUpdate: 执行进度:0% .../cn.codingblock.thread I/MyAsyncTask: taskId= 2: onProgressUpdate: 执行进度:10% .../cn.codingblock.thread I/MyAsyncTask: taskId= 2: onProgressUpdate: 执行进度:20% .../cn.codingblock.thread I/MyAsyncTask: taskId= 2: onProgressUpdate: 执行进度:30% .../cn.codingblock.thread I/MyAsyncTask: taskId= 2: onProgressUpdate: 执行进度:40% .../cn.codingblock.thread I/MyAsyncTask: taskId= 2: onProgressUpdate: 执行进度:50% .../cn.codingblock.thread I/MyAsyncTask: taskId= 2: onProgressUpdate: 执行进度:60% .../cn.codingblock.thread I/MyAsyncTask: taskId= 2: onProgressUpdate: 执行进度:70% .../cn.codingblock.thread I/MyAsyncTask: taskId= 2: onProgressUpdate: 执行进度:80% .../cn.codingblock.thread I/MyAsyncTask: taskId= 2: onProgressUpdate: 执行进度:90% .../cn.codingblock.thread I/MyAsyncTask: taskId= 2: onPostExecute: result=taskId= 2: 执行结果:100.0%可以看到两个任务是串行执行的。
使用executeOnExecutor方法
AsyncTask
executorOnExecutor方法有两个参数,第一个是Executor,第二个是任务参数。
第一个参数有两种类型:
AsyncTask.SERIAL_EXECUTOR : 表示串行执行
AsyncTask.THREAD_POOL_EXECUTOR :表示并发执行
在执行task2时调用executeOnExecutor,观察执行结果:
public class AsyncTaskActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_async_task); MyAsyncTask task1 = new MyAsyncTask(1); MyAsyncTask task2 = new MyAsyncTask(2); task1.execute(); task2.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } }log如下:
.../cn.codingblock.thread I/MyAsyncTask: taskId= 1: onPreExecute: .../cn.codingblock.thread I/MyAsyncTask: taskId= 2: onPreExecute: .../cn.codingblock.thread I/MyAsyncTask: taskId= 1: doInBackground: .../cn.codingblock.thread I/MyAsyncTask: taskId= 2: doInBackground: .../cn.codingblock.thread I/MyAsyncTask: taskId= 2: onProgressUpdate: 执行进度:0% .../cn.codingblock.thread I/MyAsyncTask: taskId= 1: onProgressUpdate: 执行进度:0% .../cn.codingblock.thread I/MyAsyncTask: taskId= 1: onProgressUpdate: 执行进度:10% .../cn.codingblock.thread I/MyAsyncTask: taskId= 2: onProgressUpdate: 执行进度:10% .../cn.codingblock.thread I/MyAsyncTask: taskId= 2: onProgressUpdate: 执行进度:20% .../cn.codingblock.thread I/MyAsyncTask: taskId= 1: onProgressUpdate: 执行进度:20% .../cn.codingblock.thread I/MyAsyncTask: taskId= 2: onProgressUpdate: 执行进度:30% .../cn.codingblock.thread I/MyAsyncTask: taskId= 1: onProgressUpdate: 执行进度:30% .../cn.codingblock.thread I/MyAsyncTask: taskId= 2: onProgressUpdate: 执行进度:40% .../cn.codingblock.thread I/MyAsyncTask: taskId= 1: onProgressUpdate: 执行进度:40% .../cn.codingblock.thread I/MyAsyncTask: taskId= 2: onProgressUpdate: 执行进度:50% .../cn.codingblock.thread I/MyAsyncTask: taskId= 1: onProgressUpdate: 执行进度:50% .../cn.codingblock.thread I/MyAsyncTask: taskId= 2: onProgressUpdate: 执行进度:60% .../cn.codingblock.thread I/MyAsyncTask: taskId= 1: onProgressUpdate: 执行进度:60% .../cn.codingblock.thread I/MyAsyncTask: taskId= 2: onProgressUpdate: 执行进度:70% .../cn.codingblock.thread I/MyAsyncTask: taskId= 1: onProgressUpdate: 执行进度:70% .../cn.codingblock.thread I/MyAsyncTask: taskId= 2: onProgressUpdate: 执行进度:80% .../cn.codingblock.thread I/MyAsyncTask: taskId= 1: onProgressUpdate: 执行进度:80% .../cn.codingblock.thread I/MyAsyncTask: taskId= 2: onProgressUpdate: 执行进度:90% .../cn.codingblock.thread I/MyAsyncTask: taskId= 2: onPostExecute: result=taskId= 2: 执行结果:100.0% .../cn.codingblock.thread I/MyAsyncTask: taskId= 1: onProgressUpdate: 执行进度:90% .../cn.codingblock.thread I/MyAsyncTask: taskId= 1: onPostExecute: result=taskId= 1: 执行结果:100.0%从log可以看到,两个任务在并发执行。
二、AsyncTask的原理