第一次写博客,写的不好请大家见谅,话说做Android开发,AsyncTask大家应该都不陌生,今天在这里给大家理一理AsyncTask的原理,先来个时序图
我们到AsyncTask中看下具体实现
public final AsyncTask
return executeOnExecutor(sDefaultExecutor, params);
}
继续查看executeOnExecutor方法的具体实现:
public final AsyncTask 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或者线程已结束运行都会抛出异常,如果线程为PENDING状态的话,会将状态设置为RUNNING,调用onPreExecute方法,如果需要在doInBackground方法执行之前作一些准备的话,可以重写这个方法
4. exec的实例化对象为SerialExecutor,其execute方法的具体实现如下:
public synchronized void execute(final Runnable r) {
mTasks.offer(new Runnable() {
public void run() {
try {
r.run();
} finally {
scheduleNext();
}
}
});
if (mActive == null) {
scheduleNext();
}
}
其中传进来的r对象为mFuture,在AsycTask构造方法中进行初始化
mWorker = new WorkerRunnable() {
public Result call() throws Exception {
mTaskInvoked.set(true);
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
//noinspection unchecked
return postResult(doInBackground(mParams));
}
};
mFuture = new FutureTask(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 occured while executing doInBackground()",
e.getCause());
} catch (CancellationException e) {
postResultIfNotInvoked(null);
}
}
};
5. FutureTask中的run方法具体实现如下:
public void run() {
sync.innerRun();
}
sync的实力对象为Sync(FutureTask的内部类)
void innerRun() {
if (!compareAndSetState(READY, RUNNING))
return;
runner = Thread.currentThread();
if (getState() == RUNNING) { // recheck after setting thread
V result;
try {
result = callable.call();
} catch (Throwable ex) {
setException(ex);
return;
}
set(result);
} else {
releaseShared(0); // cancel
}
}
其中callable其实就是mFuture 初始化中传进去的mWorker
6.callable.call()的实现也就是初始化过程中重写的那个方法
@Override
public Result call() throws Exception {
mTaskInvoked.set(true); Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
return postResult(doInBackground(mParams));
}
此时doInBackground方法就会被调用,执行完之后,会将result返回,其中的mParams在第3步中已经被初始化
7.再来看postResult实现
private Result postResult(Result result){
Message message = getHandler().obtainMessage(MESSAGE_POST_RESULT, new AsyncTaskResult(this, result));
message.sendToTarget();
return result;
}
其中getHandler会获取当前线程中对应的handler对象
8. 再来看看handMessage方法
private static class InternalHandler extends Handler{
public InternalHandler(){
super(Looper.getMainLooper());
}
@SuppressWarnings("unchecked")
@Override
public void handleMessage(Message msg) {
AsyncTaskResult> result = (AsyncTaskResult>)msg.obj;
switch(msg.what){
case MESSAGE_POST_RESULT:
result.mTask.finish(result.mData[0]);
break;
case MESSAGE_POST_PROGRESS:
result.mTask.onProgressUpdate(result.mData);
break;
}
}
}
其中mTask对象就是AsyncTask本身,其finish方法如下:
private void finish(Result result) {
if (isCancelled()) {
onCancelled(result);
} else {
onPostExecute(result);
}
mStatus = Status.FINISHED;
}
如果是用户停止的就调用onCancelled方法,否则就是任务正常结束,调用onPostExecute方法,一般都要被重写实现自己的相关业务逻辑,mStatus 状态置为FINISHED,如有问题,欢迎指出,谢谢!