AsyncTask原理
new AsyncTask() {
// 2. 运行在主线程中, 做一些准备操作.
public void onPreExecute() {
}
// 3. 运行在子线程中, 做一些耗时的任务.
public String doInBackground(String... params) {
return null;
}
// 4. 运行主线程中, result就是doInBackground方法返回的值. 做一些收尾操作.
public void onPostExecute(String result) {
}
}.execute(String... params); // 1. 开始执行异步任务.
AsyncTask的execute的方法, 代码如下:
public final AsyncTask execute(Params... params) {
...
mStatus = Status.RUNNING;
// 调用用户的实现方法, 让用户做一些准备操作. 运行在主线程中.
onPreExecute();
// 把mWorker中的mParams赋值为用户传递进来的参数.
mWorker.mParams = params;
// 使用线程池, 执行任务. 这时进入到子线程中.
sExecutor.execute(mFuture);
return this;
}
查看mWorker的初始化过程, 代码如下:
// AsyncTask构造函数在一开始使用异步任务时, 已经调用.
public AsyncTask() {
mWorker = new WorkerRunnable() {
public Result call() throws Exception {
...
}
};
// 1. 把mWorker对象传递给了FutureTask的构造函数
mFuture = new FutureTask(mWorker) {
@Override
protected void done() {
...
}
};
}
// FutureTask类中接收的是Callable的类型, 其实WorkerRunnable类型实现了Callable的类型.
public FutureTask(Callable callable) {
if (callable == null)
throw new NullPointerException();
// 2. 把callable(mWorker)传递给了Sync的构造函数.
sync = new Sync(callable);
}
Sync(Callable callable) {
// 3. 把接收过来的callable(mWorker)对象赋值给Sync类中的成员变量callable
// 总结: Sync类中的成员变量callable就是AsyncTask中的mWorker对象.
this.callable = callable;
}
mFuture对象中的run方法如下:
public void run() {
// 1. 调用了innerRun方法.
sync.innerRun();
}
void innerRun() {
if (!compareAndSetState(READY, RUNNING))
return;
runner = Thread.currentThread();
if (getState() == RUNNING) { // recheck after setting thread
V result;
try {
// 2. 调用了callable(mWorker)的call方法. 获取一个返回结果.
result = callable.call();
} catch (Throwable ex) {
setException(ex);
return;
}
// 4. 把结果传递给set方法.
set(result);
} else {
releaseShared(0); // cancel
}
}
// 在AsyncTask的构造函数中, mWorker对象初始化时, 已经覆盖了call方法, 代码如下
public Result call() throws Exception {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
// 3. 调用了用户实现的doInBackground方法, 去执行一个耗时的任务. 运行在子线程中.
// doInBackground接收的参数就是execute方法接收的参数.
return doInBackground(mParams);
}
protected void set(V v) {
// 5. 继续把参数传递给innerSet方法.
sync.innerSet(v);
}
void innerSet(V v) {
for (;;) {
int s = getState();
if (s == RAN)
return;
if (s == CANCELLED) {
// aggressively release to set runner to null,
// in case we are racing with a cancel request
// that will try to interrupt runner
releaseShared(0);
return;
}
if (compareAndSetState(s, RAN)) {
// 6. 把doInBackground返回的结果赋值给成员变量result
result = v;
releaseShared(0);
// 7. 调用FutureTask中的done方法.
done();
return;
}
}
}
// 此方法定义在AsyncTask的构造函数中, 初始化mFuture时,已经覆盖了done方法, 代码如下:
@Override
protected void done() {
Message message;
Result result = null;
try {
// 8. 调用FutureTask中的get方法获取result(doInBackground返回的结果).
result = get();
} catch (InterruptedException e) {
android.util.Log.w(LOG_TAG, e);
} catch (ExecutionException e) {
throw new RuntimeException("An error occured while executing doInBackground()",
e.getCause());
} catch (CancellationException e) {
message = sHandler.obtainMessage(MESSAGE_POST_CANCEL,
new AsyncTaskResult(AsyncTask.this, (Result[]) null));
message.sendToTarget();
return;
} catch (Throwable t) {
throw new RuntimeException("An error occured while executing "
+ "doInBackground()", t);
}
// 11. 创建一个消息对象.
// msg.what = MESSAGE_POST_RESULT;
// msg.obj = new AsyncTaskResult(AsyncTask.this, result);
message = sHandler.obtainMessage(MESSAGE_POST_RESULT,
new AsyncTaskResult(AsyncTask.this, result));
// 12. 把当前消息使用sHandler发送出去.
message.sendToTarget();
}
public V get() throws InterruptedException, ExecutionException {
// 9. 转调innerGet方法.
return sync.innerGet();
}
V innerGet() throws InterruptedException, ExecutionException {
...
// 10. 把result(doInBackground的结果)返回回去. result成员变量是在innerSet方法中赋值. 详情看第6步.
return result;
}
private static class InternalHandler extends Handler {
@SuppressWarnings({"unchecked", "RawUseOfParameterizedType"})
@Override
public void handleMessage(Message msg) {
// 把obj转换成AsyncTaskResult类, 这个类中mTask对象就是当前的AsyncTask对象. mData对象就是doInBackground的返回结果.
AsyncTaskResult result = (AsyncTaskResult) msg.obj;
switch (msg.what) {
case MESSAGE_POST_RESULT:
// There is only one result
// 13. 调用AsyncTask中的finish方法, 并且把doInBackground的返回结果传递进去.
result.mTask.finish(result.mData[0]);
break;
case MESSAGE_POST_PROGRESS:
result.mTask.onProgressUpdate(result.mData);
break;
case MESSAGE_POST_CANCEL:
result.mTask.onCancelled();
break;
}
}
}
private void finish(Result result) {
if (isCancelled()) result = null;
// 14. 把doInBackground的返回结果传递给用户实现的onPostExecute方法. 运行在主线程中, 用户可以做一些操作界面, 更新界面的操作.
onPostExecute(result);
mStatus = Status.FINISHED;
}