淺談Android中AsyncTask的工作原理
實際上,AsyncTask內(nèi)部是封裝了Thread和Handler。雖然AsyncTask很方便的執(zhí)行后臺任務(wù),以及在主線程上更新UI,但是,AsyncTask并不合適進(jìn)行特別耗時的后臺操作,對于特別耗時的任務(wù),個人還是建議使用線程池。好了,話不多說了,我們先看看AsyncTask的簡單用法吧。
AsyncTask使用方法AsyncTask是一個抽象的泛型類。簡單的介紹一下它的使用方式代碼如下:
package com.example.huangjialin.myapplication;import android.os.AsyncTask;import android.util.Log;public class AsyncTaskTest extends AsyncTask<String, Object, Long>{ @Override protected void onPreExecute() {super.onPreExecute();Log.i('AsyncTaskTest','---準(zhǔn)備下載---'); } @Override protected Long doInBackground(String... params) {Log.i('AsyncTaskTest','---在后臺正在下載---');return null; } @Override protected void onProgressUpdate(Object... values) {super.onProgressUpdate(values);Log.i('AsyncTaskTest','---在更新---'); } @Override protected void onPostExecute(Long aLong) {super.onPostExecute(aLong);Log.i('AsyncTaskTest','---下載完成,將結(jié)果返回到主線程--'); }}
然后在activity中調(diào)用 new AsyncTaskTest().execute();就可以了...使用起來比較簡單,這里就不在講述怎么使用了。
AsyncTask的4個核心方法1、onPreExecute():該方法在主線程中執(zhí)行,在執(zhí)行異步任務(wù)之前會被調(diào)用,一般用于一些準(zhǔn)備工作。
2、doInBackground(String... params):這個方法是在線程池中執(zhí)行,此方法用于執(zhí)行異步任務(wù)。在這個方法中可以通過publishProgress方法來更新任務(wù)的進(jìn)度,publishProgress方法會調(diào)用onProgressUpdate方法,另外,任務(wù)的結(jié)果返回給onPostExecute方法。
3、onProgressUpdate(Object... values):該方法在主線程中執(zhí)行,主要用于任務(wù)進(jìn)度更新的時候,該方法會被調(diào)用。
4、onPostExecute(Long aLong):在主線程中執(zhí)行,在異步任務(wù)執(zhí)行完畢之后,該方法會被調(diào)用,該方法的參數(shù)及為后臺的返回結(jié)果。
除了這幾個方法之外還有一些不太常用的方法,如onCancelled(),在異步任務(wù)取消的情況下,該方法會被調(diào)用。
好了,AsyncTask基本的使用就介紹到這里,下面我們進(jìn)入主題,我們一起看看AsyncTask的工作原理。
AsyncTask的工作原理先從execute走起,源碼來了
@MainThread public final AsyncTask<Params, Progress, Result> execute(Params... params) {return 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; }
為了方面分析,我就把英文注釋干掉了…源碼可以知道從上面的execute方法內(nèi)部調(diào)用的是executeOnExecutor()方法。而sDefaultExecutor實際上是一個串行的線程池。而onPreExecute()方法在這里就會被調(diào)用了。接著看這個線程池。
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); }} }public AsyncTask() {mWorker = new WorkerRunnable<Params, Result>() { 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; }};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);} }}; }
從上面的代碼可以知道,AsyncTask的執(zhí)行是排隊執(zhí)行的,因為有關(guān)鍵字synchronized,而AsyncTask的Params參數(shù)就封裝成為FutureTask類,F(xiàn)utureTask這個類是一個并發(fā)類,在這里它充當(dāng)了Runnable的作用。接著FutureTask會交給SerialExecutor的execute方法去處理,而SerialExecutor的executor方法首先就會將FutureTask添加到mTasks隊列中,如果這個時候沒有任務(wù),就會調(diào)用scheduleNext()方法,執(zhí)行下一個任務(wù)。如果有任務(wù)的話,則執(zhí)行完畢后最后在調(diào)用 scheduleNext();執(zhí)行下一個任務(wù)。直到所有任務(wù)被執(zhí)行完畢。而AsyncTask的構(gòu)造方法中有一個call()方法,而這個方法由于會被FutureTask的run方法執(zhí)行。所以最終這個call方法會在線程池中執(zhí)行。而doInBackground這個方法就是在這里被調(diào)用的。我們好好研究一下這個call()方法。
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; }};private Result postResult(Result result) {@SuppressWarnings('unchecked')Message message = getHandler().obtainMessage(MESSAGE_POST_RESULT,new AsyncTaskResult<Result>(this, result));message.sendToTarget();return result; }
mTaskInvoked.set(true);表示當(dāng)前任務(wù)已經(jīng)執(zhí)行過了。接著執(zhí)行doInBackground方法,最后將結(jié)果通過postResult(result);方法進(jìn)行傳遞。postResult()方法中通過sHandler來發(fā)送消息,sHandler的代碼如下:
private static class InternalHandler extends Handler {public InternalHandler() { super(Looper.getMainLooper());}@SuppressWarnings({'unchecked', 'RawUseOfParameterizedType'})@Overridepublic 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; }} }private void finish(Result result) {if (isCancelled()) { onCancelled(result);} else { onPostExecute(result);}mStatus = Status.FINISHED; }
注意:AsyncTask中有兩個線程池,一個是SerialExecutor,另一個是THREAD_POOL_EXECUTOR,其中前者主要是任務(wù)進(jìn)行排隊的,后者才是真正的執(zhí)行任務(wù)。
而AsyncTask中還有一個方法InternalHandler,這個方法的主要作用是將執(zhí)行環(huán)境從線程池切換到主線程的。
以上就是淺談Android中AsyncTask的工作原理的詳細(xì)內(nèi)容,更多關(guān)于Android中AsyncTask的工作原理的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. python爬蟲beautifulsoup解析html方法2. Python 如何將integer轉(zhuǎn)化為羅馬數(shù)(3999以內(nèi))3. python 實現(xiàn)aes256加密4. 詳解Python模塊化編程與裝飾器5. css進(jìn)階學(xué)習(xí) 選擇符6. Python性能測試工具Locust安裝及使用7. 以PHP代碼為實例詳解RabbitMQ消息隊列中間件的6種模式8. 使用Python解析Chrome瀏覽器書簽的示例9. html小技巧之td,div標(biāo)簽里內(nèi)容不換行10. python web框架的總結(jié)
