源码篇——AsyncTask机制
AsyncTask
new AsyncTask<String,String,String>(){
// 运行在主线程中,做预备工作
onPreExecute(){
}
// 运行在子线程中,做耗时操作
String doingBackGround(String s){
}
// 运行在主线程中,耗时操作完成,更新UI
onPostExecute(String s){
}
}.execute(String);
AsyncTask的execute方法
public final AsyncTask<Params, Progress, Result> execute(Params... params) {
...
mStatus = Status.RUNNING;
// 在主线程中执行准备操作
onPreExecute();
// 把params参数赋值给mWorker
mWorker.mParams = params;
// 用线程池执行mFuture
sExecutor.execute(mFuture);
// 在AsyncTask构造方法中创建了mWorker
mWorker = new WorkerRunnable<Params, Result>() {
public Result call() throws Exception {
...
}
};
mFuture = new FutureTask<Result>(mWorker) {
@Override
protected void done() {
...
}
};
}
// 把mWorker传递给FutureTask,callable指的就是mWorker
public FutureTask(Callable<V> callable) {
if (callable == null)
throw new NullPointerException();
sync = new Sync(callable);
}
// 把mWorker传递给Sync,callable指的是mWorker
Sync(Callable<V> callable) {
this.callable = callable;
}
线程池执行FutureTask,就是执行FutureTask的run方法,代码如下:
public void run() {
// 转调
sync.innerRun();
}
void innerRun() {
if (!compareAndSetState(READY, RUNNING))
return;
runner = Thread.currentThread();
if (getState() == RUNNING) { // recheck after setting thread
V result;
try {
// 就是调用了mWorker.call方法
// 把耗时操作得到的结果赋值给result
result = callable.call();
} catch (Throwable ex) {
setException(ex);
return;
}
// 转调了sync.innerSet(v);
set(result);
} else {
releaseShared(0); // cancel
}
}
mWorker = new WorkerRunnable<Params, Result>() {
public Result call() throws Exception {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
// 执行耗时操作 在子线程中执行
return doInBackground(mParams);
}
};
protected void set(V v) {
// 转调
sync.innerSet(v);
}
void innerSet(V v) {
for (;;) {
int s = getState();
if (s == RAN)
return;
if (s == CANCELLED) {
// aggressively release to set runner to null,
// in case we are racing with a cancel request
// that will try to interrupt runner
releaseShared(0);
return;
}
if (compareAndSetState(s, RAN)) {
result = v;
releaseShared(0);
// 调用了FutureTask的抽象方法
done();
return;
}
}
}
mFuture = new FutureTask<Result>(mWorker) {
@Override
protected void done() {
Message message;
Result result = null;
try {
// 转调了sync.innerGet()
result = 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) {
message = sHandler.obtainMessage(MESSAGE_POST_CANCEL,
new AsyncTaskResult<Result>(AsyncTask.this, (Result[]) null));
message.sendToTarget();
return;
} catch (Throwable t) {
throw new RuntimeException("An error occured while executing "
+ "doInBackground()", t);
}
// 发送了一个Message
message = sHandler.obtainMessage(MESSAGE_POST_RESULT,
new AsyncTaskResult<Result>(AsyncTask.this, result));
message.sendToTarget();
}
};
public V get() throws InterruptedException, ExecutionException {
// 转调
return sync.innerGet();
}
V innerGet() throws InterruptedException, ExecutionException {
acquireSharedInterruptibly(0);
if (getState() == CANCELLED)
throw new CancellationException();
if (exception != null)
throw new ExecutionException(exception);
// 把之前doinBackground方法的结果返回
return result;
}
在AsyncTask的成员变量,创建了InternalHandler
private static class InternalHandler extends Handler {
@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
// 结束耗时操作完成后的消息
// 调用了AsyncTask的finish方法传递的result.mData[0]就是之前
// 耗时操作返回来的结果
result.mTask.finish(result.mData[0]);
break;
case MESSAGE_POST_PROGRESS:
result.mTask.onProgressUpdate(result.mData);
break;
case MESSAGE_POST_CANCEL:
result.mTask.onCancelled();
break;
}
}
}
private static class AsyncTaskResult<Data> {
final AsyncTask mTask;
final Data[] mData;
// data 是返回的结果
AsyncTaskResult(AsyncTask task, Data... data) {
mTask = task;
mData = data;
}
}
private void finish(Result result) {
if (isCancelled()) result = null;
// 耗时操作完成,更新UI,执行在主线程
onPostExecute(result);
mStatus = Status.FINISHED;
}
源码篇——AsyncTask机制的更多相关文章
- 鸿蒙内核源码分析(调度机制篇) | 任务是如何被调度执行的 | 百篇博客分析OpenHarmony源码 | v7.07
百篇博客系列篇.本篇为: v07.xx 鸿蒙内核源码分析(调度机制篇) | 任务是如何被调度执行的 | 51.c.h .o 任务管理相关篇为: v03.xx 鸿蒙内核源码分析(时钟任务篇) | 触发调 ...
- 源码篇:SDWebImage
攀登,一步一个脚印,方能知其乐 源码篇:SDWebImage 源码来源:https://github.com/rs/SDWebImage 版本: 3.7 SDWebImage是一个开源的第三方库,它提 ...
- springMVC源码分析--异常处理机制HandlerExceptionResolver执行原理(二)
上一篇博客springMVC源码分析--异常处理机制HandlerExceptionResolver简单示例(一)中我们简单地实现了一个异常处理实例,接下来我们要介绍一下HandlerExceptio ...
- Dubbo 源码分析 - SPI 机制
1.简介 SPI 全称为 Service Provider Interface,是 Java 提供的一种服务发现机制.SPI 的本质是将接口实现类的全限定名配置在文件中,并由服务加载器读取配置文件,加 ...
- MyBatis 源码分析 - 插件机制
1.简介 一般情况下,开源框架都会提供插件或其他形式的拓展点,供开发者自行拓展.这样的好处是显而易见的,一是增加了框架的灵活性.二是开发者可以结合实际需求,对框架进行拓展,使其能够更好的工作.以 My ...
- spring-boot-2.0.3启动源码篇二 - run方法(一)之SpringApplicationRunListener
前言 Springboot启动源码系列还只写了一篇,已经过去一周,又到了每周一更的时间了(是不是很熟悉?),大家有没有很期待了?我会尽量保证启动源码系列每周一更,争取不让大家每周的期望落空.一周之中可 ...
- spring-boot-2.0.3启动源码篇 - 阶段总结
前言 开心一刻 朋友喜欢去按摩,第一次推门进来的是一个学生美眉,感觉还不错:后来经常去,有时是护士,有时是空姐,有时候是教师.昨天晚上推门进去的是一个女警察,长得贼好看,身材也很好,朋友嗷的一声就扑上 ...
- MySQL Master High Available 源码篇
https://m.aliyun.com/yunqi/users/1287368569594542/articles https://yq.aliyun.com/articles/59233 MySQ ...
- MyBatis 源码篇-整体架构
MyBatis 的整体架构分为三层, 分别是基础支持层.核心处理层和接口层,如下图所示. 基础支持层 反射模块 该模块对 Java 原生的反射进行了良好的封装,提供了更加简洁易用的 API ,方便上层 ...
随机推荐
- 前端小白想要编写可维护的js
我是一名前端小白,之前没写过多少代码,心里没有代码质量这个概念,人人都说代码是团队的产物,应该将代码写规范,但是我对具体什么样的代码是可维护的是茫然的. 我没写过多少代码,本来好多东西就不咋会,每次给 ...
- python3.6安装PyQt5
1.安装环境: python3.6 win8系统 2.安装方法 直接在windows cmd命令行(不需要进入python命令行模式)内输入 pip install PyQt5 等一会就行了. 用pi ...
- ●SCOI2018 AFO
Day2 T3代码: #include<cstdio> #include<cstring> #include<iostream> using namespace s ...
- [BZOJ]1177: [Apio2009]Oil
题目大意:给出一个n*m的矩阵,选出3个不相交的k*k子矩阵,使得子矩阵中元素和最大.(k<=n,m<=1500) 思路:选出的子矩阵有3种情况:横着排三个.竖着排三个.三角状分布(其中有 ...
- poj2975 Nim 胜利的方案数
Nim Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5545 Accepted: 2597 Description N ...
- bzoj4946 Noi2017 蔬菜
题目描述 小 N 是蔬菜仓库的管理员,负责设计蔬菜的销售方案. 在蔬菜仓库中,共存放有nn 种蔬菜,小NN 需要根据不同蔬菜的特性,综合考虑各方面因素,设计合理的销售方案,以获得最多的收益. 在计算销 ...
- Delphi备忘录——基本语句
选择语句: 1.If-Then-Else选择语句: If (表达式) Then Begin 语句块; End Else If(表达式) Then Begin 语句块; End; Else Begin ...
- Cisco 的基本配置实例之四----vlan的规划及配置(核心交换机)
4.vlan的规划及配置 在本节中我们讲解vlan的规划及具体的配置命令.在此例中我们用的是vtp(VLAN Trunking Protocol)server的模式,在这种模式中我们需要配置核心交换机 ...
- React .js框架的环境搭建
React学习笔记(一)- 环境搭建 最近在学习react相关的知识,刚刚起步,一路遇坑不断.自己做个笔记,方便日后总结,也供相同趣味的小伙伴一起交流探讨. 学习时主要参考官网的教程:https: ...
- idea-JSP out.println报错问题
<%! out.println("xxxx");%> 上面是错误的,<%!%>是声明变量是使用,而不是进行逻辑输出! <% out.println(x ...