AsyncTask源码阅读
来源:互联网 发布:linux五笔输入法安装 编辑:程序博客网 时间:2024/05/21 09:58
execute 方法
最终调用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; }
开始会调用我们自己实现的 onPreExecute() 然后通过exec.execute(mFuture) 的方式执行线程
构造过程
public AsyncTask() { mWorker = new WorkerRunnable<Params, Result>() { public Result call() throws Exception { mTaskInvoked.set(true); Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); //noinspection unchecked Result result = doInBackground(mParams); Binder.flushPendingCommands(); return postResult(result); } }; 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); } } }; }
mWorker 其实是callable类型 (与线程相关的一个类) 在该对象构造中调用了 我们自己实现的doInBackground(mParams); 然后调用了Binder.flushPendingCommands(); 将线程发送到内核, 最后 postResult() 发送消息给handler.
然后创建FutureTask 对象 (与线程相关的类) 调用了postResultIfNotInvoked(get()); 作用与上面一样 都调用了postResult() 只是需要在 !wasTaskInvoked 的情况下调用
0 0
- AsyncTask 源码阅读
- AsyncTask源码阅读
- AsyncTask源码阅读笔记
- 源码阅读-AsyncTask-任务调度
- (源码阅读)源码分析之AsyncTask
- Android源码阅读之——AsyncTask
- Android 阅读源码,让你彻底理解AsyncTask运行原理
- AsyncTask源码
- AsyncTask源码
- AsyncTask源码
- AsyncTask源码
- AsyncTask源码解析
- AsyncTask 源码研究
- AsyncTask 源码分析
- AsyncTask源码分析
- Android AsyncTask源码分析
- AsyncTask源码解析
- Android AsyncTask源码解析
- Sicily 1561. PRIME
- 使用Intent打开网页
- 费马小定理 Codeforces615D Multipliers
- mark,CSUST2015年校赛题目考查点
- android随笔19——手机屏幕适配
- AsyncTask源码阅读
- linux程序同时链接动态库与静态库
- 20160109 流量保护器
- 最小生成树
- python 位运算符与逻辑运算符(字符串的逻辑运算)
- C++ 之 static 关键字
- 关于继承的理解
- linux awk命令详解
- PAT程序设计考题——甲级1008(Elevator电梯时间计算问题)