以前这篇Android基础知识:多线程基础总结里提到了Android
中几种多线程任务使用方式,这篇就详细看一下其中AsyncTask
的使用和原理。android
AsyncTask
从名字就能够看出看来是Android
中提供的一个异步任务类,它的使用方法以下:
1.实现自定义AsyncTask,复写对应方法bash
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
方法以前被调用,能够在进行异步任务作一些准备工做,运行在主线程。doInBackground
方法以后被调用,得到一部人物大的返回结果,能够进行一些处理结果的操做,运行在主线程。doInBackground
方法中调用publishProgress
方法后被调用,用于异步任务时更新进度,运行在主线程。示例代码里在doInBackground
方法中模拟了耗时任务并输出了日志,经过日志能够确认复写的几个方法的执行顺序而且发现onPreExecute
、onProgressUpdate
、onPostExecute
这三个方法是运行在主线程中的,而doInBackground
方法是运行在子线程中的。这也和预期的同样,因此使用时耗时任务都是写在doInBackground
方法里的。另外AsyncTask
能够在调用execute
方法时传入所需的参数,在doInBackground
方法里能够获取到这些传入参数,而且这里的传参是个可变参数对参数个数不作限制。关于AsyncTask
的使用就是这些,接下来来看它的运行原理。ide
了解运行原理就要查看他的源码,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
给成员变量赋值。oop
接着程建立了一个WorkerRunnable
,看名字就知道应该是个工做线程,看到它复写了call
方法再结合下面又把这个WorkerRunnable
传入了一个FutureTask
能够猜想,它是由一个Callable
实现的,点击跟踪找一下这个类,他是一个内部类。post
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
不清楚的能够去看前一篇多线程基础中有简单的介绍。ui
在进行下一步调用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
的运行原理就所有讲完了,最后经过两张图梳理下整个过程。
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
进行更新。
关于这点一样仍是源码版本差别,新版本源码从以前的运行原理描述中能够看到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
这个任务线程池中的。
在使用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
方法是不能阻止内存泄漏的。
关于AsyncTask
的内容就是以上这些了,AsyncTask
做为官方提供的一种处理异步任务的封装类,其实也并无那么好用,稍不注意就会发生各类问题,不过做为了解,理解它的运行原理仍是颇有必要的。