Android AsyncTask源码解读

屡思路

1. 初始 AsyncTask

AsyncTask 这个类的声明如下:


public abstract class AsyncTask {
  .....
}

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

2.使用

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

* 

Here is an example of subclassing:

*
 * private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
 *     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");
 *     }
 * }
 * 
//user

Once created, a task is executed very simply:

*
 * new DownloadFilesTask().execute(url1, url2, url3);
 * 

3. 内部重要方法

  • onPreExecute()
@MainThread
  protected void onPreExecute() {
   }

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

  • doInBackground()
@WorkerThread
 protected abstract Result doInBackground(Params... params);

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

  • onProgressUpdate()
@MainThread
    protected void onProgressUpdate(Progress... values) {
    }

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

  • onPostExecute()
@MainThread
    protected void onPostExecute(Result result) {
    }

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

4.源码分析

从它的 execute 方法开始:

 @MainThread
    public final AsyncTask execute(Params... params) {
      //sDefaultExecutor 定义如下,线程池
        return executeOnExecutor(sDefaultExecutor, params);
    }

private static volatile Executor sDefaultExecutor = SERIAL_EXECUTOR;
public static final Executor SERIAL_EXECUTOR = new SerialExecutor();
@MainThread
    public final AsyncTask executeOnExecutor(Executor exec,
            Params... params) {
      //首先判断是不是 PENDING
        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)");
            }
        }
      //将状态设置为 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 mWorker;
//抽象类 并且实现了Callable 接口
private static abstract class WorkerRunnable implements Callable {
        Params[] mParams;
    }

    @SuppressWarnings({"RawUseOfParameterizedType"})
    private static class AsyncTaskResult {
        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() {
          //实现 了 call 方法
            public Result call() throws Exception {
              //设置调用了为 true
                mTaskInvoked.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 {
                  //最后执行postResult
                    postResult(result);
                }
              //返回结果
                return result;
            }
        };

        mFuture = new FutureTask(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);
                }
            }
        };
    }

postResult(result) 方法

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

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

@SuppressWarnings({"RawUseOfParameterizedType"})
    private static class AsyncTaskResult {
        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"})
        @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;
            }
        }
    }

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

下面看下 finish方法:

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

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

  • mFuture
private final FutureTask mFuture;

//初始化也是在AsyncTask 构造方法中执行的,在mWorker 之下,并且将mWorder 传入
mFuture = new FutureTask(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);
                }
            }
        };

postResultIfNotInvoked(get()); 如下:

private void postResultIfNotInvoked(Result result) {
        final boolean wasTaskInvoked = mTaskInvoked.get();
      //wasTaskInvoked 为true ,之前在 mWorker 中设置了为true
//mWorker = new WorkerRunnable() {
//            public Result call() throws Exception {
 //               mTaskInvoked.set(true);

        if (!wasTaskInvoked) {
            postResult(result);
        }
    }

FutureTask :

我们知道mWorker implement Callable 接口,传入赋值给了callable 变量
 public FutureTask(Callable 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 变量又赋值给了 c
            Callable 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 interrupts
            int 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 mTasks = new ArrayDeque();
        Runnable mActive;

        public synchronized void execute(final Runnable r) {
            mTasks.offer(new Runnable() {
                public void run() {
                    try {
                        r.run();
                    } finally {
                        scheduleNext();
                    }
                }
            });
          //第一次肯定为null ,执行 scheduleNext
            if (mActive == null) {
                scheduleNext();
            }
        }

        protected synchronized void scheduleNext() {
          //给 mActivie 赋值,mTasks.poll 会从第一个开始取
            if ((mActive = mTasks.poll()) != null) {
                THREAD_POOL_EXECUTOR.execute(mActive);
            }
        }
    }

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

/**
      插入一个 element 在队尾
     * Inserts the specified element at the end of this deque.
     *
     * 

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 方法:

@WorkerThread
    protected final void publishProgress(Progress... values) {
        //如果没取消
        if (!isCancelled()) {
    //会发送一个 MESSAGE_POST_PROGRESS 的消息            getHandler().obtainMessage(MESSAGE_POST_PROGRESS,
                    new AsyncTaskResult(this, values)).sendToTarget();
        }
    }
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://是他是他 就是他
                    //3. 调用了 onProgressUpdate 方法了
                    result.mTask.onProgressUpdate(result.mData);
                    break;
            }
        }
    }

会调用 AsyncTaskonProgressUpdate 方法了。结束。

你可能感兴趣的:(Android AsyncTask源码解读)