摘要:Android AsyncTask原理解析:AsyncTask就是对了耗时操作时的线程调度和任务状态管理,其中部分的代码写法还是值得学习的,希望对Android开发的学习有所帮助。
AsyncTask就是对了耗时操作时的线程调度和任务状态管理,其中部分的代码写法还是值得学习的,希望对Android开发的学习有所帮助。
基本原理
AsyncTask的基本原理是:线程池 + Handler,内部封装了2个线程池和1个Handler;线程池负责线程调度和执行任务,Handler负责异步通信。
成员变量
具体作用
THREAD_POOL_EXECUTOR(线程池) 执行任务的线程
sBackupExecutor(线程池) 使用队列,配合任务调度
mHandler 工作线程与主线程之间的通信
主要回调方法
onPreExecute()
doInBackground(Params... params)
onProgressUpdate(Progress... values)
onPostExecute(Result result)
onCancelled(Result result)
这是整个流程中关键节点的方法,在调用execute()方法之后,按照顺序onPreExecute() -> doInBackground(Params... params) -> onPostExecute(Result result).
如果调用cancel方法取消的当前任务,最后会执行onCancelled(Result result)替代onPostExecute
在doInBackground方法中还可以可以调用onProgressUpdate(Progress... values)方法更新任务进度。
2.分析
AsyncTask构造器
AsyncTask的构造器分别创建了Handler,WorkerRunnable和FutureTask一个对象。
源码中的的构造器,有三个,但是实际能使用的只有AsyncTask(),后两个都被hide了无法调用,这一点就限制了Handler在创建的时候,一定是在UI线程的。
AsyncTask()
AsyncTask(Handler handler)
AsyncTask(Looper callbackLooper)
这个内部默认的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;
}
}
}
可以看出,主要就是发送结果和更新progress事件。
WorkerRunnable其实是用来暂存Params[]的Callable接口,Callable接口只有call()
private static abstract class WorkerRunnable<Params, Result> implements Callable<Result> {
Params[] mParams;
}
在创建WorkerRunnable对象时,实现了call()方法
mWorker = new WorkerRunnable<Params, Result>() {
public Result call() throws Exception {
//1.修改Task是否执行为true
mTaskInvoked.set(true);
Result result = null;
try {
//2.设置线程优先级
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
//3.执行doInBackground方法,获取结果
result = doInBackground(mParams);
Binder.flushPendingCommands();
} catch (Throwable tr) {
//4.发生异常,修改是否取消状态为true
mCancelled.set(true);
throw tr;
} finally {
//5.同过mHandler发送result
postResult(result);
}
return result;
}
};
创建了1个FutureTask类的对象,复写了done(),这里注意FutureTask在创建的时候传入WorkerRunnable的示例对象,在FutureTask的run方法中会执行WorkerRunnable的call方法
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) {
//获取task执行的标记,没有执行则通过mHandler发送结果
final boolean wasTaskInvoked = mTaskInvoked.get();
if (!wasTaskInvoked) {
postResult(result);
}
}
FutureTask是实现了Runnable和Future接口的类,主要是维护了Task在整个过程中的一个状态,在execute()方法中最终执行的就是FutureTask的示例对象。
execute()
execute其实是调用了executeOnExecutor方法,将task加入到任务队列的线程池中,然后按顺序执行
对于mStatus这个状态可以看到,每个AsyncTask只能执行一次,执行中或者已经完成的AsyncTask在此执行都会抛出IllegalStateException
@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) {
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就是对了耗时操作时的线程调度和任务状态管理,虽然现在基本不使用,但是部分的代码写法还是值得学习的。
作者:MrTrying
链接:https://juejin.im/post/6869674367618383886
来源:掘金
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号