一 什么是AsnycTask

AsyncTask enables proper and easy use of the UI thread. This class allows you to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

这句话的意思是,我们在UI线程可以很简单、适用的调用AsyncTask。这个方法可以使我们不用调用Thread+handler的方式,就可以轻松的在后台线程做操作,然后把结果回调给UI线程

AsyncTask is designed to be a helper class around {@link Thread} and {@link Handler}

  • and does not constitute a generic threading framework. AsyncTasks should ideally be
  • used for short operations (a few seconds at the most.) If you need to keep threads
  • running for long periods of time, it is highly recommended you use the various APIs
  • provided by the java.util.concurrent package such as {@link Executor},
  • {@link ThreadPoolExecutor} and {@link FutureTask}.

同时,google也声明了,AsyncTask设计的初衷就是一个用来连接Threa和Handler的帮助类,而不是用于设计一个通用的线程框架,同时,AsnycTask应该用于那些后台操作在极短,最长时间不超过几秒的操作,如果我们需要在后台进行长时间的操作,更推荐使用{@link Executor},{@link ThreadPoolExecutor} and {@link FutureTask}(也即是自定义线程池)去定义自己的后台行为

二 几个重要方法和变量

public final AsyncTask execute(Params... params) {    return executeOnExecutor(sDefaultExecutor, params);}public final AsyncTask 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;}

其中execute(Params...params)方法,调用的最终是回归到executeOnExecutor(Executor exec,Params... params)方法中,其中,传入的参数是sDefaultExecutor中,我们回归源码,看到这个sDefaultExecutor是什么?

 /** * 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();private static volatile Executor sDefaultExecutor = SERIAL_EXECUTOR;`

好了,也即是说,sDefaultExecutor其实是一个一个串行执行的executor,如果你在使用execute(Params... params)的时候,执行的代码是:

new AsyncTask() {        @Override        protected Void doInBackground(Void... params) {            Log.i(MainActivity.this.getClass().getSimpleName(),"execute time "+System.currentTimeMillis());            return null;        }    }.execute();    new AsyncTask() {        @Override        protected Void doInBackground(Void... params) {            Log.i(MainActivity.this.getClass().getSimpleName(),"execute time "+System.currentTimeMillis());            return null;        }    }.execute();  

那么,你的执行结果是这样的:

10-22 17:35:33.279 25394-25463/com.example.hao_wu.myapplication I/MainActivity: execute time 150866493327910-22 17:35:33.280 25394-25466/com.example.hao_wu.myapplication I/MainActivity: execute time 1508664933280

所以,如果你需要一个并发执行的AsyncTask,那么你需要在execute执行的时候,指定需要调用的executor (AsyncTask.THREAD_POOL_EXECUTOR)

    new AsyncTask() {        @Override        protected Void doInBackground(String... params) {            Log.i(MainActivity.this.getClass().getSimpleName(), "execute time " + System.currentTimeMillis());            return null;        }    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "");    new AsyncTask() {        @Override        protected Void doInBackground(String... params) {            Log.i(MainActivity.this.getClass().getSimpleName(),"execute time "+System.currentTimeMillis());            return null;        }    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,""); 

这个时候,我们来看一下线程的执行情况:

10-22 17:45:49.045 2254-2393/com.example.hao_wu.myapplication I/MainActivity: execute time 150866554904510-22 17:45:49.045 2254-2395/com.example.hao_wu.myapplication I/MainActivity: execute time 1508665549045

综上所述,AsnycTask 提供了两个线程池,一个SERIAL_EXECUTOR,用于控制AsyncTask线程的串行执行,也可以说,AsnycTask用这个线程池来实现排队队列;另一个是AsyncTask.THREAD_POOL_EXECUTOR,用于真正的执行线程.

三 言归正传

AsyncTask为我们提供四个核心的回调方法,我们来看一下当我们初始化一个AsnycTask时,会给我们回调什么方法:

    new AsyncTask() {        @Override        protected void onPreExecute() {            super.onPreExecute();        }        @Override        protected Void doInBackground(Void... params) {            return null;        }        @Override        protected void onPostExecute(Void aVoid) {            super.onPostExecute(aVoid);        }        @Override        protected void onProgressUpdate(Void... values) {            super.onProgressUpdate(values);        }    }.execute();  

上面的回调方法中,我们着重看一下doInbackground()方法,通过翻看源码我们可以看到,

public AsyncTask() {    mWorker = new WorkerRunnable() {        public Result call() throws Exception {            mTaskInvoked.set(true);            Result result = null;            try {                Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);                //noinspection unchecked                result = doInBackground(mParams);                Binder.flushPendingCommands();            } catch (Throwable tr) {                mCancelled.set(true);                throw tr;            } finally {                postResult(result);            }            return result;        }    };    ...}  

也即是说,当我们在线程开始执行的时候,会回调doInBackground方法给调用方,当方法结束的时候,AsnycTask通过postResult(result)方法,把结果通过handler回调给UI线程

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

AsnycTask定义了一个InnerHandler,去接受这个message

private static class InternalHandler extends Handler {    public InternalHandler() {        super(Looper.getMainLooper());    }    @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;        }    }}

而后呢,AsnycTask在finish方法中,把线程结束的信息回调给 onPostExecute(result)

private void finish(Result result) {    if (isCancelled()) {        onCancelled(result);    } else {        onPostExecute(result);    }    mStatus = Status.FINISHED;}

自此,AsnycTask中源码的调用流程就已经分析完了,以下是几个应该注意的地方:

1 AsnycTask应该在UI线程中被初始化和使用,因为创建Handler对象时需要当前线程的Looper,所以我们为了以后能够通过sHandler将执行环境从后台线程切换到主线程,我们必须使用主线程的Looper,因此必须在主线程中创建sHandler。

2 AsnycTask定义了两个线程池线程数,分别是:

private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();// We want at least 2 threads and at most 4 threads in the core pool,// preferring to have 1 less than the CPU count to avoid saturating// the CPU with background workprivate 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;

官方的解释是:
我们至少需要2个线程,最多需要4个线程在核心池中,采用比cpu数少一个的方式,避免后台工作的CPU饱和
另外,定义的两个线程池数量的作用是:
1、如果线程池的当前大小还没有达到基本大小(CORE_POOL_SIZE < MAXIMUM_POOL_SIZE),那么就新增加一个线程处理新提交的任务;
2、如果当前大小已经达到了基本大小,就将新提交的任务提交到阻塞队列排队,等候处理;
3、如果队列容量已达上限,并且当前大小CORE_POOL_SIZE没有达到MAXIMUM_POOL_SIZE,那么就新增线程来处理任务;
4、如果队列已满,并且当前线程数目也已经达到上限,那么意味着线程池的处理能力已经达到了极限,此时需要拒绝新增加的任务

更多相关文章

  1. Android(安卓)App 调用自带浏览器
  2. 数据存储之SQLite
  3. android 异步通信简单小计
  4. 为何Android普通APP可以执行私有数据中的so文件,而system app却不
  5. Android(安卓)GifImageView加载Gif图片及原理
  6. Android9 framework 按键音调用流程及自定义按键音(替换原生按键
  7. Android在Service服务中调用Activity活动的方法,实现UI界面更新操
  8. Android并发编程 多线程与锁
  9. Android全面解析之Context机制

随机推荐

  1. Android(安卓)文本阅读以及TextView指定
  2. Android UI开发第九篇——SlidingDrawer
  3. Android: 你必须掌握的Android命令
  4. android Spinner和数值选择器使用demo
  5. android经典DEMO
  6. android中Invalidate和postInvalidate的
  7. android 不销毁当前activity,返回上一层,亲
  8. Android:PopupMenu显示item中的android:i
  9. Android Button 样式 圆角
  10. MTK Android(安卓)N 源码Rom Root