1.前言
之前这篇Android基础知识:多线程基础总结里提到了Android中几种多线程任务使用方式,这篇就详细看一下其中AsyncTask的使用和原理。
2. 基础使用
AsyncTask从名字就可以看出看来是Android中提供的一个异步任务类,它的使用方法如下:
1.实现自定义AsyncTask,复写对应方法
class MyAsyncTask extends AsyncTask<String, Integer, String> {
int percent = 0;
@Override
protected void onPreExecute() {
//doInBackground执行之前
Log.d(TAG, "onPreExecute:" + Thread.currentThread().getName() + " 异步任务准备开始 ");
}
@Override
protected void onProgressUpdate(Integer... values) {
//doInBackground执行中
Log.d(TAG, "onProgressUpdate:" + Thread.currentThread().getName() + " 任务进行中:" + values[0] + "%");
}
@Override
protected void onPostExecute(String result) {
//doInBackground执行之后
Log.d(TAG, "onPostExecute:" + Thread.currentThread().getName() + " " + result);
}
@Override
protected String doInBackground(String... strings) {
String param = strings[0];
Log.d(TAG, "doInBackground:" + Thread.currentThread().getName() + " 异步任务执行中,获得参数:" + param);
while (percent < 100) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
percent += 10;
publishProgress(percent);
}
Log.d(TAG, "doInBackground:" + Thread.currentThread().getName() + " 异步任务完成!");
return "任务完成啦";
}
}
2.创建自定义的AsyncTask对象,调用execute方法开始任务
MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute("测试");
运行结果:

AsyncTask它有三个泛型,分别对应开始传入的参数类型,中间进度类类型和最后任务结束返回的结果类型,实现时需要根据要求进行设置。另外还有四个主要复写的方法分别是doInBackground、onPreExecute、onProgressUpdate和onPostExecute。
- doInBackground:看名知意,是负责执行异步任务的方法,运行在后台子线程,是必须复写的方法。
- onPreExecute:在
doInBackground方法之前被调用,可以在进行异步任务做一些准备工作,运行在主线程。 - onPostExecute:在
doInBackground方法之后被调用,获得一部人物大的返回结果,可以进行一些处理结果的操作,运行在主线程。 - onProgressUpdate:在
doInBackground方法中调用publishProgress方法后被调用,用于异步任务时更新进度,运行在主线程。
示例代码里在doInBackground方法中模拟了耗时任务并输出了日志,通过日志可以确认复写的几个方法的执行顺序并且发现onPreExecute、onProgressUpdate、onPostExecute这三个方法是运行在主线程中的,而doInBackground方法是运行在子线程中的。这也和预期的一样,所以使用时耗时任务都是写在doInBackground方法里的。另外AsyncTask可以在调用execute方法时传入所需的参数,在doInBackground方法里可以获取到这些传入参数,并且这里的传参是个可变参数对参数个数不做限制。关于AsyncTask的使用就是这些,接下来来看它的运行原理。
3.运行原理
了解运行原理就要查看他的源码,AsyncTask源码不多,一共不到800行就实现了需要的功能。我们按照AsyncTask的使用步骤来看它的源码流程。在实现自己定义的AsyncTask后,第一个步骤就是创建自定义的AsyncTask对象,所以就从构造函数开始看起。
public AsyncTask() {
this((Looper) null);
}
public AsyncTask(@Nullable Handler handler) {
this(handler != null ? handler.getLooper() : null);
}
public AsyncTask(@Nullable Looper callbackLooper) {
// 给mHandler初始化赋值
mHandler = callbackLooper == null || callbackLooper == Looper.getMainLooper()
? getMainHandler()
: new Handler(callbackLooper);
// 创建WorkerRunnable
mWorker = new WorkerRunnable<Params, Result>() {
public Result call() throws Exception {
mTaskInvoked.set(true);
Result result = null;
try {
// 设置线程优先级 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
//调用doInBackground方法并获得返回结果
result = doInBackground(mParams);
Binder.flushPendingCommands();
} catch (Throwable tr) {
mCancelled.set(true);
throw tr;
} finally {
// 将返回结果post传递
postResult(result);
}
return result;
}
};
// 创建FutureTask传入WorkerRunnable
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);
}
}
};
}
AsyncTask有三个构造函数,最常使用的是无参的构造函数,当然也可以调用有参的传入一个Handler或者Looper,不管调用那个最终都会走到参数是Looper的这个构造中来,这个构造函数中首先对成员变量中的mHandler进行赋值,赋值先判断了传入的Looper是否为空,如果为空或者传入的Looper是主线程的Looper就调用getMainHandler直接将主线程的Handler赋值给mHandler,否则就用传入的Looper创建一个Handler给成员变量赋值。
接着程创建了一个WorkerRunnable,看名字就知道应该是个工作线程,看到它复写了call方法再结合下面又把这个WorkerRunnable传入了一个FutureTask可以猜测,它是由一个Callable实现的,点击跟踪找一下这个类,他是一个内部类。
private static abstract class WorkerRunnable<Params, Result> implements Callable<Result> {
Params[] mParams;
}
可以看到就是一个实现了Callable接口的抽象类,其中封装了我们的传入参数。回到构造函数中WorkerRunnable的call方法中显示设置了当前线程的优先级为后台进程,接着执行了doInBackground方法获得异步任务的返回结果,最后在finally代码块中调用postResult方法将结果传递。
构造函数中的最后一步是创建了一个异步任务对象FutureTask将WorkRunnable传入,在异步任务完成时会调用done方法,复写的done方法中同样先通过get方法获得异步任务结果,再通过postResultIfNotInvoked方法将结果传递。关于Callable和FutureTask不清楚的可以去看前一篇多线程基础中有简单的介绍。
在进行下一步调用execute方法之前,先看一下AsyncTask类还有一个静态代码块,我们知道静态代码块会随类的加载而加载,所以先来看下这个静态代码块。
// CPU核心数
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
//线程池核心线程数
private static final int CORE_POOL_SIZE = Math.max(2, Math.min(CPU_COUNT - 1, 4));
// 线程池最大线程数
private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
// 线程池中线程空闲存活实现
private static final int KEEP_ALIVE_SECONDS = 30;
// 线程工厂
private static final ThreadFactory sThreadFactory = new ThreadFactory() {
// 原子型指令Integer类型
private final AtomicInteger mCount = new AtomicInteger(1);
// 返回一个名为AsyncTask #加上编号的线程
public Thread newThread(Runnable r) {
return new Thread(r, "AsyncTask #" + mCount.getAndIncrement());
}
};
// 线程池的阻塞队列长度128
private static final BlockingQueue<Runnable> sPoolWorkQueue =
new LinkedBlockingQueue<Runnable>(128);
/**
* An {@link Executor} that can be used to execute tasks in parallel.
*/
public static final Executor THREAD_POOL_EXECUTOR;
static {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_SECONDS, TimeUnit.SECONDS,
sPoolWorkQueue, sThreadFactory);
threadPoolExecutor.allowCoreThreadTimeOut(true);
THREAD_POOL_EXECUTOR = threadPoolExecutor;
}
静态代码块中初始化了一个线程池,并将其赋值给成员变量中的THREAD_POOL_EXECUTOR,从注释看是一个用于执行并行任务的线程池。下面就进入execute方法来看看AsyncTask到底是怎么运行的。
public final AsyncTask<Params, Progress, Result> execute(Params... params) {
return executeOnExecutor(sDefaultExecutor, params);
}
execute方法中进而调用了executeOnExecutor方法,除了传入了params还传入了一个executeOnExecutor,先来看看executeOnExecutor是什么?
private static volatile Executor sDefaultExecutor = SERIAL_EXECUTOR;
/**
* An {@link Executor} that executes tasks one at a time in serial
* order. This serialization is global to a particular process.
*/
public static final Executor SERIAL_EXECUTOR = new SerialExecutor();
它引用了成员变量中的SERIAL_EXECUTOR,而SERIAL_EXECUTOR又是一个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 {
// 调用传入的Runnable的run方法
r.run();
} finally {
scheduleNext();
}
}
});
// 第一次进入或对列为空mActive为空直接调用scheduleNext
if (mActive == null) {
scheduleNext();
}
}
// 从队列中取出任务交给THREAD_POOL_EXECUTOR线程池处理
protected synchronized void scheduleNext() {
if ((mActive = mTasks.poll()) != null) {
THREAD_POOL_EXECUTOR.execute(mActive);
}
}
}
SerialExecutor中有一个任务队列mTasks,它的execute方法中将传入的Runnable重新封装入一个新的Runnable,在新的Runnable的run方法中调用原来Runnable的run方法,并且在finally代码块中调用了scheduleNext方法,最终将这个新的Runnable任务加入mTasks任务队列。之后判断了mActive是否为空,为空的话也调用scheduleNext方法。这个mActive看到是在scheduleNext方法中从任务队列中取出的Runnable对象,scheduleNext方法中取出任务后不为空将这个任务交给THREAD_POOL_EXECUTOR这个线程池去处理,THREAD_POOL_EXECUTOR就是静态代码块中初始化的线程池,也是最终处理异步任务的线程池。
接着再回到executeOnExecutor方法中,AsyncTask的execute方法中调用的executeOnExecutor方法。
@MainThread
public final AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec,
Params... params) {
// 判断AsyncTask的状态不为PENDING抛出对应异常
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)");
}
}
// 状态为PENDING就将状态修改为RUNNING
mStatus = Status.RUNNING;
// 调用onPr方法eExecute
onPreExecute();
// 将execute方法中传入的参数传递到构造函数中创建的WorkerRunnable中
mWorker.mParams = params;
// 将构造函数中创建的FutureTask交给exec线程池即sDefaultExecutor线程池处理
exec.execute(mFuture);
return this;
}
方法中先判断AsyncTask的状态,AsyncTask的状态有以下几种。
public enum Status {
/**
* 表示任务尚未执行
*/
PENDING,
/**
* 表示任务正在执行
*/
RUNNING,
/**
* 表示任务已经完成
*/
FINISHED,
}
AsyncTask的不是尚未执行状态就抛出对应的异常,否则就调用onPreExecute这个异步任务执行前的方法,然后将接收到的参数传入构造函数中创WorkerRunnable中,最后调用exec.execute方法将异步任务交给exec线程池即sDefaultExecutor线程池处理。接着就会按照之前看过的一样,异步任务会先进入SerialExecutor线程池,在SerialExecutor中放入取出串行任务队列,最终交给THREAD_POOL_EXECUTOR线程池执行任务。
之前看过WorkRunnable任务执行完成后会调用postResult或postResultIfNotInvoked方法。
private void postResultIfNotInvoked(Result result) {
final boolean wasTaskInvoked = mTaskInvoked.get();
if (!wasTaskInvoked) {
postResult(result);
}
}
postResultIfNotInvoked方法中还是调用了postResult方法,所以进入这个postResult方法跟踪查看。
private Result postResult(Result result) {
@SuppressWarnings("unchecked")
Message message = getHandler().obtainMessage(MESSAGE_POST_RESULT,
new AsyncTaskResult<Result>(this, result));
message.sendToTarget();
return result;
}
postResult方法中调用了getHandler方法获得成员变量中的mHandler发送了个消息,消息中将异步任务的返回结果封装成了一个AsyncTaskResult对象放入消息的obj发送。AsyncTaskResult对象将是将当前的AsyncTask和异步任务结果做了个封装。
private static class AsyncTaskResult<Data> {
final AsyncTask mTask;
final Data[] mData;
AsyncTaskResult(AsyncTask task, Data... data) {
mTask = task;
mData = data;
}
}
还记得mHandler是在哪儿创建的吗?是在AsyncTask的构造函数中通过getMainHandler方法创建的。
private static Handler getMainHandler() {
synchronized (AsyncTask.class) {
if (sHandler == null) {
sHandler = new InternalHandler(Looper.getMainLooper());
}
return sHandler;
}
}
getMainHandler方法中用主线程Looper创建一个InternalHandler。刚才的postResult方法中发送的消息,就交给的是这个Handler处理。
private static class InternalHandler extends Handler {
public InternalHandler(Looper looper) {
super(looper);
}
@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中的handleMessage方法中处理了两种消息,一种是异步任务完成结果的消息,另一种是更新进度的消息。还是先来看任务结果的消息,其对应MESSAGE_POST_RESULT。调用result.mTask.finish方法,即AsyncTask的finish方法。
private void finish(Result result) {
if (isCancelled()) {
onCancelled(result);
} else {
onPostExecute(result);
}
mStatus = Status.FINISHED;
}
finish方法中先判断了此时任务有没有被取消,取消了就会走onCancelled方法,没取消就会调用onPostExecute这个异步任务完成后的方法,然后将AsyncTask的状态修改为已完成。至此AsyncTask的一次执行流程结束。
最后来看看异步任务更新进度的方法。要进行进度更新需要我们在doInBackground方法中调用publishProgress(percent)方法将进度传入。于是来看这个publishProgress(percent)方法。
protected final void publishProgress(Progress... values) {
if (!isCancelled()) {
getHandler().obtainMessage(MESSAGE_POST_PROGRESS,
new AsyncTaskResult<Progress>(this, values)).sendToTarget();
}
}
方法中先判断任务没被取消就获取Handler发送更新进度消息,进度数据同样封装到AsyncTaskResult类型里。然后再看怎么处理消息。
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;
}
}
处理消息很简单就是调用了AsyncTask的onProgressUpdate方法将进度数据传入即可。至此AsyncTask的运行原理就全部讲完了,最后通过两张图梳理下整个过程。


4.AsyncTask的问题
4.1 AsyncTask必须在主线程创建调用
AsyncTask一定要在主线程创建调用吗?实践是检验真理的唯一标准。
new Thread(new Runnable() {
@Override
public void run() {
MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute("测试");
}
}).start();
运行结果日志:

AsyncTask并没有发生错误,只是onPreExecute方法调用的线程变了,变成创建的子线程了,这也很好理解,根据之前看过的源码可以知道onPreExecute方法是在executeOnExecutor方法中直接调用的,所以就是运行在AsyncTask创建线程里。而其他方法要么是通过线程池中线程调用,要么是通过InternalHandler到主线程中调用,所以运行线程与之前无异。
那么子线程中创建使用AsyncTask单单只是onPreExecute方法运行线程不同吗?为什么会有必须在主线程创建调用这种说法呢?
其实这种说法是建立在老版本的AsyncTask上的,在上面的运行原理中所有的源码相关都是截取的API28版本的AsyncTask的源码。其中Handler是获得的主线程的Looper创建的。
sHandler = new InternalHandler(Looper.getMainLooper());
而在早期版本中,这里看API10版本的AsyncTask中的代码。
private static final InternalHandler sHandler = new InternalHandler();
早期版本中创建Handler时没有使用主线程的Looper所以创建AsyncTask的线程就是Handler所在线程,这样会导致onPostExecute和onProgressUpdate方法都会运行在这个线程,如果创建AsyncTask不在主线程就会导致onPostExecute和onProgressUpdate方法也不运行在主线程,进而在这两个方法中无法直接对UI进行更新。
4.2 Android3.0之前AsyncTask是并行执行任务,3.0之后为串行执行。
关于这点同样还是源码版本差异,新版本源码从之前的运行原理描述中可以看到AsyncTask在调用execute方法开启任务后,是先将任务放入一个SerialExecutor线程池,其中维护了一个队列,每次都是从这个队列中一个个取任务再交给真正处理任务的线程池。而在早期版本中是没有这个SerialExecutor这个线程池的,任务都是直接放入任务线程池执行的。
public final AsyncTask<Params, Progress, Result> execute(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;
sExecutor.execute(mFuture);
return this;
}
private static final ThreadPoolExecutor sExecutor = new ThreadPoolExecutor(CORE_POOL_SIZE,
MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, sWorkQueue, sThreadFactory);
上面这段是API10对应Android2.3.7版本中AsyncTask的execute方法,可以看到任务是直接加入到sExecutor这个任务线程池中的。
4.3 AsyncTask内存泄漏
在使用AsyncTask若为非静态内部类,在Activity销毁时,AsyncTask中的耗时任务还没有完成,这时AsyncTask会还持有Activity的引用,造成其无法被会正常回收,造成内存泄漏。
解决方法也很简单只要使用静态类static加上弱引用WeakReference就可以了。另外从源码中看出AsyncTask有提供一个cancel方法,那么在Activity的destory方法里调用这个方法取消任务可不可以解决内存泄漏呢?答案是不可以的,具体还是看源码。
private final AtomicBoolean mCancelled = new AtomicBoolean();
public final boolean cancel(boolean mayInterruptIfRunning) {
mCancelled.set(true);
return mFuture.cancel(mayInterruptIfRunning);
}
看到AsyncTask的cancel方法只是将mCancelled这个状态修改为true,另外调用了mFuture.cancel方法。
public boolean cancel(boolean mayInterruptIfRunning) {
if (!(state == NEW &&
U.compareAndSwapInt(this, STATE, NEW,
mayInterruptIfRunning ? INTERRUPTING : CANCELLED)))
return false;
try { // in case call to interrupt throws exception
if (mayInterruptIfRunning) {
try {
Thread t = runner;
if (t != null)
t.interrupt();
} finally { // final state
U.putOrderedInt(this, STATE, INTERRUPTED);
}
}
} finally {
finishCompletion();
}
return true;
}
在mFuture.cancel方法中最终还是调用了线程的t.interrupt方法,而Thread的interrupt方法是不会立即中断线程的,它同样是仅仅修改了一个中断线程的标志状态,需要自己在代码中判断处理或者等该线程进入阻塞才会抛出一个InterruptedException异常退出线程。所以AsyncTask的cancel方法是不能阻止内存泄漏的。
5.总结
关于AsyncTask的内容就是以上这些了,AsyncTask作为官方提供的一种处理异步任务的封装类,其实也并没有那么好用,稍不注意就会发生各种问题,不过作为了解,理解它的运行原理还是很有必要的。