AsyncTask 相信大家对他的使用并不陌生,它是谷歌官方提供的一种轻量级别的异步任务类,底层封装的是两个线程池和一个Handler.sDefaultExecutor=SERIAL_EXECUTOR 用来Runnable排队用的, THREAD_POOL_EXECUTOR 是用来真正执行任务的.
我们先从 execute()开始分析,executeOnExecutor(Executor exec,
Params... params) 真正执行的代码如下:
if (mStatus != Status.PENDING) {
switch (mStatus) {
case RUNNING:
throw new IllegalStateException("Cannot execute task:"
+ " the task is already running.");
case FINISHED:
throw new IllegalStateException("Cannot execute task:"
+ " the task has already been executed "
+ "(a task can be executed only once)");
}
}
mStatus = Status.RUNNING;
onPreExecute();
mWorker.mParams = params;
exec.execute(mFuture);
return this;
}
如果执行状态是Running(正在执行)或Finish(已经执行完了),抛出异常,所以一个AsyncTask 只能执行一次. 接着调用onPreExcute(),这就是为什么onPreEcxute是在执行实际的后台操作前被UI thread调用. 任务的真正执行是在exec.execute(mFuture) 中, exec 是 SerialExecutor 的一个对象,我们就去看看SerialExecutor 是如何工作的,代码如下:
private static class SerialExecutor implements Executor {
final ArrayDeque<Runnable> mTasks = new ArrayDeque<Runnable>();
Runnable mActive;
public synchronized void execute(final Runnable r) {
mTasks.offer(new Runnable() {
public void run() {
try {
r.run();
} finally {
scheduleNext();
}
}
});
if (mActive == null) {
scheduleNext();
}
}
protected synchronized void scheduleNext() {
if ((mActive = mTasks.poll()) != null) {
THREAD_POOL_EXECUTOR.execute(mActive);
}
}
}
我们可以知道AsyncTask在构造方法中把Params 参数转化为一个FutureTask,接着SerialExecutor 会把FutureTask对象插入到ArrayDeque 任务队列,ArrayDeque 内部使用数组存储数据,大小自增长的队列.从这里可以看书AsyncTask 是串行执行的,最终交给scheduleNext()去执行.
public AsyncTask() {
mWorker = new WorkerRunnable<Params, Result>() {
public Result call() throws Exception {
mTaskInvoked.set(true);
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
//noinspection unchecked
Result result = doInBackground(mParams);
Binder.flushPendingCommands();
return postResult(result);
}
};
mFuture = new FutureTask<Result>(mWorker) {
@Override
protected void done() {
try {
postResultIfNotInvoked(get());
} catch (InterruptedException e) {
android.util.Log.w(LOG_TAG, e);
} catch (ExecutionException e) {
throw new RuntimeException("An error occurred while executing doInBackground()",
e.getCause());
} catch (CancellationException e) {
postResultIfNotInvoked(null);
}
}
};
}
我们在public AsyncTask() {}构造方法中可以得知mWorker 首先将mTaskInvoked.set(true) 接着调用doInBackground(mParams) 返回Result,最后将其返回值传递给postResult(result).
private Result postResult(Result result) {
@SuppressWarnings("unchecked")
Message message = getHandler().obtainMessage(MESSAGE_POST_RESULT,
new AsyncTaskResult<Result>(this, result));
message.sendToTarget();
return result;
}
postResult ()获取一个InternalHandler ,通过InternalHandler 发送MESSAGE_POST_RESULT的消息,最终由InternalHandler 来处理消息
private static class InternalHandler extends Handler {
public InternalHandler() {
super(Looper.getMainLooper());
}
@SuppressWarnings({"unchecked", "RawUseOfParameterizedType"})
@Override
public void handleMessage(Message msg) {
AsyncTaskResult<?> result = (AsyncTaskResult<?>) msg.obj;
switch (msg.what) {
case MESSAGE_POST_RESULT:
// There is only one result
result.mTask.finish(result.mData[0]);
break;
case MESSAGE_POST_PROGRESS:
result.mTask.onProgressUpdate(result.mData);
break;
}
}
}
为了能让执行环境切换到主线程,这就要求InternalHandler 必须在主线中,所以AsyncTask必须在主线程中创建.
以上就是AsyncTask的工作原理分析.
通过以上分析我们可以得知:
- Task的实例必须在UI thread中创建
- execute方法必须在UI thread中调用
- 不要手动的调用onPreExecute(), onPostExecute(Result),doInBackground(Params...), onProgressUpdate(Progress...)这几个方法
- 该task只能被执行一次,否则多次调用时将会出现异常。
如果在子线程中创建调用 onPreExecute()也在创建AsyncTask的子线程中执行,doInBackground(Params...)在子线程中执行,onPostExecute(Result)和onProgressUpdate(Progress...)在主线程中
由于本人第一次写源码分析的文章,再加上本人表达能力有限,如若哪里有误,,还望多多拍砖,请与指正,谢谢