屡思路

1. 初始 AsyncTask

AsyncTask 这个类的声明如下:


public abstract class AsyncTask<Params, Progress, Result> {.....
}
复制代码

是一个抽象类 Params 表示输入参数的类型 Progress 表示后台任务的执行进度 Result 表示返回结果的类型

2.使用

AsyncTask 这个类的顶部有一些代码注释,里面讲述了如何使用一个 AsyncTask,如下:

* <p>Here is an example of subclassing:</p>* <pre class="prettyprint">* private class DownloadFilesTask extends AsyncTask&lt;URL, Integer, Long&gt; {*     protected Long doInBackground(URL... urls) {*         int count = urls.length;*         long totalSize = 0;*         for (int i = 0; i < count; i++) {*             totalSize += Downloader.downloadFile(urls[i]);*             publishProgress((int) ((i / (float) count) * 100));*             // Escape early if cancel() is called*             if (isCancelled()) break;*         }*         return totalSize;*     }**     protected void onProgressUpdate(Integer... progress) {*         setProgressPercent(progress[0]);*     }**     protected void onPostExecute(Long result) {*         showDialog("Downloaded " + result + " bytes");*     }* }* </pre>//user
<p>Once created, a task is executed very simply:</p>* <pre class="prettyprint">* new DownloadFilesTask().execute(url1, url2, url3);* </pre>
复制代码

3. 内部重要方法

  • onPreExecute()
@MainThreadprotected void onPreExecute() {}
复制代码

在主线程中运行,异步任务之前会被调用,一般用于做一些准备工作;

  • doInBackground()
@WorkerThreadprotected abstract Result doInBackground(Params... params);
复制代码

在线程池中运行,此方法一般用于执行异步任务,通过publishProgress 方法来更新进度;

  • onProgressUpdate()
@MainThreadprotected void onProgressUpdate(Progress... values) {}
复制代码

主线程中运行,当通过publishProgress 方法调用后,onProgressUpdate() 方法会被调用;

  • onPostExecute()
@MainThreadprotected void onPostExecute(Result result) {}
复制代码

主线程中运行,将返回的结果展示。

4.源码分析

从它的 execute 方法开始:

 @MainThreadpublic final AsyncTask<Params, Progress, Result> execute(Params... params) {//sDefaultExecutor 定义如下,线程池return executeOnExecutor(sDefaultExecutor, params);}private static volatile Executor sDefaultExecutor = SERIAL_EXECUTOR;
public static final Executor SERIAL_EXECUTOR = new SerialExecutor();
复制代码
@MainThreadpublic final AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec,Params... params) {//首先判断是不是 PENDINGif (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)");}}//将状态设置为 RUNNING 状态mStatus = Status.RUNNING;//1.调用了 onPreExecute() 方法onPreExecute();//将参数封装到 mWorker.mParams 中去了mWorker.mParams = params;//调用execute 将mFuture 传进去了exec.execute(mFuture);return this;}
复制代码

为了弄明白整体流程,首页要搞明白上面的 mWorker mFuture 是干嘛的。

  • mWorker
 private final WorkerRunnable<Params, Result> mWorker;
//抽象类 并且实现了Callable 接口
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;}}
}
复制代码
//在AsyncTask 的构造方法中,分别对 mWorker, mFuture 进行了初始化
public AsyncTask(@Nullable Looper callbackLooper) {mHandler = callbackLooper == null || callbackLooper == Looper.getMainLooper()? getMainHandler(): new Handler(callbackLooper);mWorker = new WorkerRunnable<Params, Result>() {//实现 了 call 方法public Result call() throws Exception {//设置调用了为 truemTaskInvoked.set(true);//Result result = null;try {//设置线程的优先级Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);//noinspection unchecked//将 2. doInBackground的结果存储到 result 中result = doInBackground(mParams);Binder.flushPendingCommands();} catch (Throwable tr) {mCancelled.set(true);throw tr;} finally {//最后执行postResultpostResult(result);}//返回结果return result;}};mFuture = new FutureTask<Result>(mWorker) {@Overrideprotected 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);}}};}
复制代码

postResult(result) 方法

 private Result postResult(Result result) {@SuppressWarnings("unchecked")Message message = getHandler().obtainMessage(MESSAGE_POST_RESULT,new AsyncTaskResult<Result>(this, result));message.sendToTarget();return result;}复制代码

发送一条 MESSAGE_POST_RESULT 的消息,并且将result 存入到了 AsyncTaskResult中的 mData 中去了, 其中 AsyncTaskResult

@SuppressWarnings({"RawUseOfParameterizedType"})private static class AsyncTaskResult<Data> {final AsyncTask mTask;final Data[] mData;AsyncTaskResult(AsyncTask task, Data... data) {mTask = task;mData = data;}}
复制代码

getHandler 获取一个 Handler ,我们看下 handleMessageMESSAGE_POST_RESULT 对这条消息的处理:

 private static class InternalHandler extends Handler {public InternalHandler(Looper looper) {super(looper);}@SuppressWarnings({"unchecked", "RawUseOfParameterizedType"})@Overridepublic void handleMessage(Message msg) {AsyncTaskResult<?> result = (AsyncTaskResult<?>) msg.obj;switch (msg.what) {case MESSAGE_POST_RESULT://是他是他 就是他// There is only one resultresult.mTask.finish(result.mData[0]);break;case MESSAGE_POST_PROGRESS:result.mTask.onProgressUpdate(result.mData);break;}}}
复制代码

其中 resultAsyncTaskResult 类型,前面我们见到过的,mTask 当时我们传的是 this 也就是当前的 AsyncTask ,调用finish 方法,将mData 返回的结果传入进去,还记得我们前面看过的吗,将返回的结果存入AsyncTaskResult.mData中去了。

下面看下 finish方法:

private void finish(Result result) {//判断是否取消,如果取消了,就不执行onPostExecute 了if (isCancelled()) {onCancelled(result);} else {//4. 就执行onPostExecute 方法了onPostExecute(result);}// 将状态标志为 finishmStatus = Status.FINISHED;}
复制代码

ok ,上述都是 mWorker 工作的,接下来是我们一开始说的 mFuture

  • mFuture
private final FutureTask<Result> mFuture;//初始化也是在AsyncTask 构造方法中执行的,在mWorker 之下,并且将mWorder 传入
mFuture = new FutureTask<Result>(mWorker) {@Overrideprotected 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);}}};复制代码

postResultIfNotInvoked(get()); 如下:

private void postResultIfNotInvoked(Result result) {final boolean wasTaskInvoked = mTaskInvoked.get();//wasTaskInvoked 为true ,之前在 mWorker 中设置了为true
//mWorker = new WorkerRunnable<Params, Result>() {
//            public Result call() throws Exception {//               mTaskInvoked.set(true);if (!wasTaskInvoked) {postResult(result);}}
复制代码

FutureTask :

我们知道mWorker implement Callable 接口,传入赋值给了callable 变量public FutureTask(Callable<V> callable) {if (callable == null)throw new NullPointerException();this.callable = callable;this.state = NEW;       // ensure visibility of callable}public void run() {if (state != NEW ||!U.compareAndSwapObject(this, RUNNER, null, Thread.currentThread()))return;try {//callable 变量又赋值给了 cCallable<V> c = callable;if (c != null && state == NEW) {V result;boolean ran;try {//这里调用c.call 实际上就是调用 mWorker.call 方法
//,由我们上面的分析知道,在mWorker.call 方法中最终会返回 result 结果result = c.call();ran = true;} catch (Throwable ex) {result = null;ran = false;setException(ex);}if (ran)set(result);}} finally {// runner must be non-null until state is settled to// prevent concurrent calls to run()runner = null;// state must be re-read after nulling runner to prevent// leaked interruptsint s = state;if (s >= INTERRUPTING)handlePossibleCancellationInterrupt(s);}}
复制代码

ok ,这是 mFuture,还剩下最后一个:

exec.execute(mFuture);

exec 就是 sDefaultExecutor ,其实 就是 SerialExecutor,如下:

private static volatile Executor sDefaultExecutor = SERIAL_EXECUTOR;public static final Executor SERIAL_EXECUTOR = new 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 {r.run();} finally {scheduleNext();}}});//第一次肯定为null ,执行 scheduleNextif (mActive == null) {scheduleNext();}}protected synchronized void scheduleNext() {//给 mActivie 赋值,mTasks.poll 会从第一个开始取if ((mActive = mTasks.poll()) != null) {THREAD_POOL_EXECUTOR.execute(mActive);}}}
复制代码

上面我们将 mFuture 传入,实际就是 r. mTaskArrayDeque<Runnable> 姑且认为它是这个排队序列的吧。看下offer 方法:

/**插入一个 element 在队尾* Inserts the specified element at the end of this deque.** <p>This method is equivalent to {@link #offerLast}.** @param e the element to add* @return {@code true} (as specified by {@link Queue#offer})* @throws NullPointerException if the specified element is null*/public boolean offer(E e) {return offerLast(e);}
复制代码

看注释,也就是说是每次执行一个任务,都是在当前 deque 的队尾开始排队的。并且执行是串行的,因为当第二个线程过来的时候,判断 mActive 不为 null 将不会执行 scheduleNext.(我这个是8.0)源码,其实在 android 3.0 之后 AsyncTask 都是采用串行执行任务的。

各个版本的不同如下: android 1.6之前 ------ 串行 android 1.6-3.0 之间 ----- 并行 android 3.0 之后 ----- 串行

尽管如此,我们仍然可以通过 调用 executeOnExecutor 来并行执行任务。

ok , 回到那个 execute 方法中,我们说调用了 r.run 实际山就是 调用 mFuture.run 方法: 上面我们展示过在 mFuture.run 方法中如下:

try {//这里调用c.call 实际上就是调用 mWorker.call 方法
//,由我们上面的分析知道,在mWorker.call 方法中最终会返回 result 结果result = c.call();ran = true;} catch (Throwable ex) {result = null;ran = false;setException(ex);}
复制代码

最终调用mWorker.call 方法,而在 mWorker.call 方法中,我们完成一系列的任务,调用了 doInBackground onPostExecute 完成了整个的调用过程。

有的人可能已经注意到了 还差一个 onProgressUpdate 方法还没被调用,我们知道只有调用那个 publishProgress 方法的时候才能调用 onProgressUpdate ,那下面我们卡夏 publishProgress 方法:

@WorkerThreadprotected final void publishProgress(Progress... values) {//如果没取消if (!isCancelled()) {//会发送一个 MESSAGE_POST_PROGRESS 的消息            getHandler().obtainMessage(MESSAGE_POST_PROGRESS,new AsyncTaskResult<Progress>(this, values)).sendToTarget();}}
复制代码
private static class InternalHandler extends Handler {public InternalHandler(Looper looper) {super(looper);}@SuppressWarnings({"unchecked", "RawUseOfParameterizedType"})@Overridepublic void handleMessage(Message msg) {AsyncTaskResult<?> result = (AsyncTaskResult<?>) msg.obj;switch (msg.what) {case MESSAGE_POST_RESULT:// There is only one resultresult.mTask.finish(result.mData[0]);break;case MESSAGE_POST_PROGRESS://是他是他 就是他//3. 调用了 onProgressUpdate 方法了result.mTask.onProgressUpdate(result.mData);break;}}}
复制代码

会调用 AsyncTaskonProgressUpdate 方法了。结束。

Android AsyncTask源码解读相关推荐

  1. Android AsyncTask源码解析

    在开发Android应用时必须遵守单线程模型的原则: Android UI操作并不是线程安全的并且这些操作必须在UI线程中执行.在单线程模型中始终要记住两条法则: 1. 不要阻塞UI线程 2. 确保只 ...

  2. android asynctask源码分析,Android通过Handler与AsyncTask两种方式动态更新ListView(附源码)...

    本文实例讲述了Android通过Handler与AsyncTask两种方式动态更新ListView的方法.分享给大家供大家参考,具体如下: 有时候我们需要修改已经生成的列表,添加或者修改数据,noti ...

  3. android单元测试作用,Android单元测试源码解读

    Android手机操作系统是一个开源的操作系统.程序员们可以在模拟器的帮助下对其进行修改,来实现各种功能需求,满足用户的应用.在这里我们先来了解一下Android单元测试的相关内容. 在网络上找了半天 ...

  4. Android 开源框架之 Android-async-http 源码解读

    开源项目链接 Android-async-http仓库:https://github.com/loopj/android-async-http android-async-http主页:http:// ...

  5. PackageManagerService Android 8.1 源码解读 02

    接上文:PackageManagerService Android 8.1 源码解读 01 d.第三步细节:PKMS.main(),main函数主要工作: [检查]Package编译相关系统属性 [调 ...

  6. android 普通蓝牙源码解读

    一:概述 上一篇博客讲了下蓝牙4.0在android中的应用,这里讲讲普通蓝牙之间如何操作. 我记得在大二的时候还做了个比较烂的游戏,当时喜欢打dota就做了个蓝牙之间对战坦克的游戏,可以去看看,确实 ...

  7. PackageManagerService Android 8.1 源码解读 01

    一.PackageManagerService 是什么? 答: PackageManagerService(简称 [PKMS]),是 Android 系统中核心服务之一,负责应用程序的安装,卸载,信息 ...

  8. 【Android】OkHttp源码解读逐字稿(1)-拦截器

    目录 0.前言 1.OkHttp的简单使用 2.浅析开始 拦截器 链式调用流程示意图 第 0 个拦截器 第一个 拦截器 RetryAndFollowUpInterceptor 第二个拦截器 Bridg ...

  9. 【Android】OkHttp源码解读逐字稿(2)-OkHttpClient

    目录 0.前言 1.各个属性浅析 01.dispatcher 02.connectionPool 03.interceptors&networkInterceptors 04.eventLis ...

最新文章

  1. 在Ubuntu 14.04 64bit上使用pycURL模块示例
  2. 堵住大数据“窃密通道”刻不容缓
  3. 祝51CTO 生日快乐
  4. PostgreSQL 异步IO实测
  5. 神盾特工hive_周末剧透:杰西卡·琼斯、黑名单、时空守卫等
  6. DC-DC电源PCB设计指南
  7. 使用PowerShell 链接Azure 查看信息
  8. HTML+CSS的学习
  9. python中strip是什么意思啊_Python中的strip()的理解
  10. WAP自助建站 我编程之路的启蒙
  11. np.unique()官方文档分析以及举例
  12. 【转】从事IT行业的应该如何学习最高效的休息方式
  13. pytest文档24-fixture的作用范围(scope)
  14. ckplay video.php,CKPLAYER播放器的教程-小白看
  15. python提取文字的轮廓_opencv 轮廓提取文字
  16. 利用C++,设置输入某年某月某日,判断这一天是这一年的第几天。
  17. 达梦DM单机部署以及日常运维管理
  18. 基于CANdelaStudio制作CDD流程概述
  19. Redis(基于狂神说Java-Redis)
  20. 网段、子网号、网络地址、网络号辨析

热门文章

  1. MySQL - 高效的设计MySQL库表
  2. Elasticsearch-Jest 配置ES集群源码解读
  3. Spring Cloud【Finchley】-06服务消费者整合Feign
  4. Spring Cache抽象-使用Java类注解的方式整合EhCache
  5. 信息提醒之Toast-更新中
  6. 递归 累加和累乘
  7. wdcp导出mysql_phpmyadmin导入导出mysql(只适用WDCP系统)
  8. 大型网站 linux,大型网站架构演变
  9. CSS学习16之层级
  10. 斐波那契的数列的计算python-python斐波那契数列的计算方法