AsyncTask 工做原理分析

AsyncTask 相信你們對他的使用並不陌生,它是谷歌官方提供的一種輕量級別的異步任務類,底層封裝的是兩個線程池和一個Handler.sDefaultExecutor=SERIAL_EXECUTOR 用來Runnable排隊用的, THREAD_POOL_EXECUTOR 是用來真正執行任務的.android

咱們先從 execute()開始分析,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;
}

若是執行狀態是Running(正在執行)或Finish(已經執行完了),拋出異常,因此一個AsyncTask 只能執行一次. 接着調用onPreExcute(),這就是爲何onPreEcxute是在執行實際的後臺操做前被UI thread調用. 任務的真正執行是在exec.execute(mFuture) 中, exec 是 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();
                }
            }
        });
        if (mActive == null) {
            scheduleNext();
        }
    }

    protected synchronized void scheduleNext() {
        if ((mActive = mTasks.poll()) != null) {
            THREAD_POOL_EXECUTOR.execute(mActive);
        }
    }
}

咱們能夠知道AsyncTask在構造方法中把Params 參數轉化爲一個FutureTask,接着SerialExecutor 會把FutureTask對象插入到ArrayDeque 任務隊列,ArrayDeque 內部使用數組存儲數據,大小自增加的隊列.從這裏能夠看書AsyncTask 是串行執行的,最終交給scheduleNext()去執行.ide

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](https://my.oschina.net/u/1162528)
        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);
            }
        }
    };
}

咱們在public AsyncTask() {}構造方法中能夠得知mWorker 首先將mTaskInvoked.set(true) 接着調用doInBackground(mParams) 返回Result,最後將其返回值傳遞給postResult(result).oop

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

postResult ()獲取一個InternalHandler ,經過InternalHandler 發送MESSAGE_POST_RESULT的消息,最終由InternalHandler 來處理消息源碼分析

private static class InternalHandler extends Handler {
    public InternalHandler() {
        super(Looper.getMainLooper());
    }

    @SuppressWarnings({"unchecked", "RawUseOfParameterizedType"})
    [@Override](https://my.oschina.net/u/1162528)
    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;
        }
    }
}

爲了能讓執行環境切換到主線程,這就要求InternalHandler 必須在主線中,因此AsyncTask必須在主線程中建立.post

以上就是AsyncTask的工做原理分析. 經過以上分析咱們能夠得知:this

    1. Task的實例必須在UI thread中建立
  1. execute方法必須在UI thread中調用
  2. 不要手動的調用onPreExecute(), onPostExecute(Result),doInBackground(Params...), onProgressUpdate(Progress...)這幾個方法
  3. 該task只能被執行一次,不然屢次調用時將會出現異常。 若是在子線程中建立調用 onPreExecute()也在建立AsyncTask的子線程中執行,doInBackground(Params...)在子線程中執行,onPostExecute(Result)和onProgressUpdate(Progress...)在主線程中

因爲本人第一次寫源碼分析的文章,再加上本人表達能力有限,如若哪裏有誤,,還望多多拍磚,請與指正,謝謝.net

相關文章
相關標籤/搜索