AsyncTask()解析(6.0)
public AsyncTask() {
mWorker = new WorkerRunnable<Params, Result>() {
public Result call() throws Exception {
mTaskInvoked.set(true);
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
return postResult(doInBackground(mParams));
}
};
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 occured while executing doInBackground()",e.getCause());
} catch (CancellationException e) {
postResultIfNotInvoked(null);
}
}
};
}
private static abstract class WorkerRunnable<Params, Result> implements Callable<Result> {
Params[] mParams;
}
这一步初始化了一个Callable子类的WorkerRunnable对象和一个FutureTask对象,然后就没有然后了。AsyncTask的精华都在execute()。当然,等下还要回来分析这里的代码,暂时先跳过。
AsyncTask#execute()解析
public final AsyncTask<Params, Progress, Result> execute(Params... params) {
return executeOnExecutor(sDefaultExecutor, params);
}
execute()内部只是调用了executeOnExecutor(),并指定了第一个参数sDefaultExecutor。剧透下:sDefaultExecutor就是AsyncTask串行执行的原因,后面会详细讲这个。先跟进executeOnExecutor()。
public final AsyncTask<Params, Progress, Result> 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;
}
首先对任务状态的判断,这里体现了同一个AsyncTask不能多次调用execute()方法,否则会报异常的原因。也就意味着即将被执行的任务只能处于等待(PENDING)状态。之后改变任务的状态为运行中(RUNNING),之后调用onPreExecute(),这个方法是AsyncTask暴露给子类的方法,一般在此方法中做一些准备工作,例如显示个进度条。之后调用sDefaultExecutor.execute(mFuture)。在AsyncTask()中已经出现过mFuture,mFuture初始化的时候传进来的是Callable的子类WorkerRunnable的对象。是时候回头仔细看下mWorker这个对象了。
mWorker = new WorkerRunnable<Params, Result>() {
public Result call() throws Exception {
mTaskInvoked.set(true);
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
return postResult(doInBackground(mParams));
}
};
首先将标志位mTaskInvoked设置为true,之后设置线程优先级为后台线程。然后调用doInBackground(),doInBackground()是个抽象方法,子类必须实现。一般我们在此方法中调用耗时操作,例如:下载文件。之后将返回的参数Result当做参数传递给postResult。跟进。
private Result postResult(Result result) {
Message message = getHandler().obtainMessage(MESSAGE_POST_RESULT,new AsyncTaskResult<Result>(this, result));
message.sendToTarget();
return result;
}
private static InternalHandler sHandler;
private static Handler getHandler() {
synchronized (AsyncTask.class) {
if (sHandler == null) {
sHandler = new InternalHandler();
}
return sHandler;
}
}
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;
}
}
}
注意下这里的sHandler是个静态成员变量,而且,获取单例的时候,传递的是MainLooper。这也就意味着从子线程到主线程的切换,即InternalHandler#handleMessage()会在主线程调用。InternalHandler收到MESSAGE_POST_RESULT消息之后调用AsyncTask#finish()。跟进。
private void finish(Result result) {
if (isCancelled()) {
onCancelled(result);
} else {
onPostExecute(result);
}
mStatus = Status.FINISHED;
}
一般认为AsyncTash暴露出来四个方法给子类调用:onPreExecute()、doInBackground()、onProgressUpdate()、onPostExecute()。其实还有个不常用的方法onCancelled(String result)及重载方法onCancelled()。在这里首先会判断是否已经取消了这个任务,如果已经取消,执行onCancelled(),否则执行onPostExecute()。这里也能体现一个道理,就算调用task.cancel(true)取消任务,实际上任务还是在执行的,只是得不到进度及最后的通知回调。一般在onPostExecute()方法中做一些收尾工作,比如:隐藏加载动画。之后,将任务的状态设置成结束状态(FINISHED)。分析到这里,似乎没有看见更新进度onProgressUpdate()的调用。这是因为手动调用publishProgress()之后才会调用onProgressUpdate()方法。跟进。
protected final void publishProgress(Progress... values) {
if (!isCancelled()) {
getHandler().obtainMessage(MESSAGE_POST_PROGRESS,
new AsyncTaskResult<Progress>(this, values)).sendToTarget();
}
}
...
case MESSAGE_POST_PROGRESS:
result.mTask.onProgressUpdate(result.mData);
break;
如果没有取消任务,则切换到主线程执行onProgressUpdate()。到这里,AsyncTask五个子类方法调用流程就解析完成了。
AsyncTask高级解析
串行是怎么实现的?
刚开始,这个问题也迷惑了我一会儿。因为每次都是新建一个AsyncTask对象,讲道理应该是并行的。知道我看见了SerialExecutor的修饰词static。
sDefaultExecutor强引用了SerialExecutor的静态实例SERIAL_EXECUTOR。跟进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);
}
}
}
这段代码蛮有意思的。SerialExecutor内部使用了一个队列来存储所有的任务,调用execute()方法会将传递进来的Runnable对象包装之后加入mTasks队列,这里是个什么设计模式来着,很熟悉,一下想不起来了。现在一步步分析这段代码。
第一次调用SerialExecutor#execute()
先将Runnable对象加入队列,然后判断当前任务对象mActive(肯定为null,即会调用scheduleNext()方法)。
第n(n>1)次调用SerialExecutor#execute()
- 当前任务对象mActive不为null,只将Runnable对象加入队列。
- 当前任务对象mActive为null,同理。
综上,无论第几次调用在SerialExecutor#execute(),scheduleNext()最多只有一个入口。在scheduleNext()中,首先从任务队列mTask中取出一个任务,如果不为空,则将当前任务对象mActive添加进线程池执行。前面说过,mActive对真正的Runnable进行了包装,内部调用Runnable#run之后再次调用scheduleNext()。正是这种递归的形式导致了AsyncTask的串行。那么有没有办法并行的?当然有。
AsyncTask并行操作
通过上述分析,可以发现:AsyncTask的并行主要是因为SerialExecutor在作怪。实际上在executeOnExecutor()中,可以传递自定义的Executor实现并行操作。AsyncTask提供了一个THREAD_POOL_EXECUTOR串行执行任务,实际上,这个THREAD_POOL_EXECUTOR是支持并行的。也就是说我们只要调用
new MyAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, param);
这样就能实现并行操作。当然,也可以传入自定义的Executor。那么问题来了:既然AsyncTask可以实现并行操作,为什么还要才去串行这种方式?在并行操作时,当任务过多的时候,AsyncTask会出现 AsyncTask的成员函数doInBackground调用不及时,出现delay的情况。即doInBackground没有在onPreExecute调用后立即被执行。因此还是不建议使用并行这种方式。
AsyncTask源码中文注释解析
public abstract class AsyncTask<Params, Progress, Result> {
private static final String LOG_TAG = "AsyncTask";
// 当前的cpu核心数
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
// 线程池核心容量
private static final int CORE_POOL_SIZE = CPU_COUNT + 1;
// 线程池最大容量
private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
// 存活时间
private static final int KEEP_ALIVE = 1;
private static final ThreadFactory sThreadFactory = new ThreadFactory() {
private final AtomicInteger mCount = new AtomicInteger(1);
public Thread newThread(Runnable r) {
return new Thread(r, "AsyncTask #" + mCount.getAndIncrement());
}
};
private static final BlockingQueue<Runnable> sPoolWorkQueue =
new LinkedBlockingQueue<Runnable>(128);
// 静态并发线程池,可以用来并行执行任务
public static final Executor THREAD_POOL_EXECUTOR
= new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE,
TimeUnit.SECONDS, sPoolWorkQueue, sThreadFactory);
public static final Executor SERIAL_EXECUTOR = new SerialExecutor();
private static final int MESSAGE_POST_RESULT = 0x1;
private static final int MESSAGE_POST_PROGRESS = 0x2;
// 默认任务执行者,串行
private static volatile Executor sDefaultExecutor = SERIAL_EXECUTOR;
// 持有MainLooper的Handler
private static InternalHandler sHandler;
// Callable子类
private final WorkerRunnable<Params, Result> mWorker;
// 在mWorker执行完成后,实现具体的逻辑
private final FutureTask<Result> mFuture;
// 线程默认等待执行状态
private volatile Status mStatus = Status.PENDING;
// 是否被取消
private final AtomicBoolean mCancelled = new AtomicBoolean();
// 是否被调用
private final AtomicBoolean mTaskInvoked = new AtomicBoolean();
// 串行执行者 ps:AsyncTask的精华就在这里
private static class SerialExecutor implements Executor {
// 存放等待执行任务的队列
final ArrayDeque<Runnable> mTasks = new ArrayDeque<Runnable>();
// 当前正在执行的任务
Runnable mActive;
public synchronized void execute(final Runnable r) {
// 将任务插入等待队列,并且在真正调用逻辑代码之后调用scheduleNext()实现串行
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);
}
}
}
public enum Status {
// 等待执行
PENDING,
// 正在执行
RUNNING,
// 执行结束
FINISHED,
}
private static Handler getHandler() {
synchronized (AsyncTask.class) {
if (sHandler == null) {
sHandler = new InternalHandler();
}
return sHandler;
}
}
/** @hide */
public static void setDefaultExecutor(Executor exec) {
sDefaultExecutor = exec;
}
public AsyncTask() {
mWorker = new WorkerRunnable<Params, Result>() {
public Result call() throws Exception {
mTaskInvoked.set(true);
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
//noinspection unchecked
// 调用doInBackground()方法
Result result = doInBackground(mParams);
Binder.flushPendingCommands();
// 通过Handler调用onPostExecute()或者onCancelled()
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);
}
}
};
}
private void postResultIfNotInvoked(Result result) {
final boolean wasTaskInvoked = mTaskInvoked.get();
if (!wasTaskInvoked) {
postResult(result);
}
}
// 通过Handler调用onPostExecute()或者onCancelled()
private Result postResult(Result result) {
@SuppressWarnings("unchecked")
Message message = getHandler().obtainMessage(MESSAGE_POST_RESULT,
new AsyncTaskResult<Result>(this, result));
message.sendToTarget();
return result;
}
// 获取任务执行状态。等待(PENDING),正在执行(RUNNING),结束(FINISHED)
public final Status getStatus() {
return mStatus;
}
// 在子线程中调用。抽象方法,子类必须实现。
@WorkerThread
protected abstract Result doInBackground(Params... params);
// 主线程调用,子类可选择是否实现
@MainThread
protected void onPreExecute() {
}
// 主线程调用,任务执行完毕后调用,子类可选择是否实现
@SuppressWarnings({"UnusedDeclaration"})
@MainThread
protected void onPostExecute(Result result) {
}
// 主线程调用,更新进度,子类可选择是否实现
@SuppressWarnings({"UnusedDeclaration"})
@MainThread
protected void onProgressUpdate(Progress... values) {
}
// 取消任务,调用task.cancel(true)后会调用
@SuppressWarnings({"UnusedParameters"})
@MainThread
protected void onCancelled(Result result) {
onCancelled();
}
@MainThread
protected void onCancelled() {
}
public final boolean isCancelled() {
return mCancelled.get();
}
public final boolean cancel(boolean mayInterruptIfRunning) {
mCancelled.set(true);
return mFuture.cancel(mayInterruptIfRunning);
}
public final Result get() throws InterruptedException, ExecutionException {
return mFuture.get();
}
public final Result get(long timeout, TimeUnit unit) throws InterruptedException,
ExecutionException, TimeoutException {
return mFuture.get(timeout, unit);
}
// 执行任务
@MainThread
public final AsyncTask<Params, Progress, Result> execute(Params... params) {
return executeOnExecutor(sDefaultExecutor, params);
}
// 真正执行任务的地方(划重点)
@MainThread
public final AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec,
Params... params) {
// 验证任务执行状态,防止execute()被多次调用
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;
}
@MainThread
public static void execute(Runnable runnable) {
sDefaultExecutor.execute(runnable);
}
// 一般在doInBackground()中调用,内部通过Hanlder调用onProgressUpdate()实现更新UI
@WorkerThread
protected final void publishProgress(Progress... values) {
if (!isCancelled()) {
getHandler().obtainMessage(MESSAGE_POST_PROGRESS,
new AsyncTaskResult<Progress>(this, values)).sendToTarget();
}
}
// 结束任务,在Hanlder中调用
private void finish(Result result) {
// 判断任务是否被取消
if (isCancelled()) {
// 执行暴露的取消方法
onCancelled(result);
} else {
// 执行暴露的执行完毕方法
onPostExecute(result);
}
// 更改任务状态为结束
mStatus = Status.FINISHED;
}
private static class InternalHandler extends Handler {
public InternalHandler() {
// 传递的是主线程的Looper(划重点)
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;
}
}
}
private static abstract class WorkerRunnable<Params, Result> implements Callable<Result> {
Params[] mParams;
}
@SuppressWarnings({"RawUseOfParameterizedType"})
private static class AsyncTaskResult<Data> {
final AsyncTask mTask;
final Data[] mData;
AsyncTaskResult(AsyncTask task, Data... data) {
mTask = task;
mData = data;
}
}
}
更多Framework源码解析,请移步 Framework源码解析系列[目录]