小葵花妈妈课堂开课了:《AsyncTask源码分析》
生活随笔
收集整理的這篇文章主要介紹了
小葵花妈妈课堂开课了:《AsyncTask源码分析》
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
AsyncTask 官方介紹:
在UI線程使用AsyncTask是適當和簡單的。 這個類允許你在UI線程中不使用多線程或者Handers的情況下,就能執行后臺操作和發布結果。AsyncTask是圍繞Thread和Handler來設計的幫助類,不構成通用線程框架。 AsyncTasks通常理想情況下用來執行簡短的操作(最多就是幾秒鐘)。 如果你需要保持線程跑很長時間,就推薦你使用java.util.concurrent包下面的Executor、ThreadPoolExecutor、FutureTask各種api。一個異步任務是需要通過計算來定義,也就是任務跑在后臺線程而結果會在UI線程發布。 異步任務定義有3個泛型類型,Prarams、Progress、Result;并且有4個步驟,叫做 onPreExecute、doInBackground、onProgressUpdate、onPostExecuteAsyncTask必須子類化才可以使用。子類需要至少重寫1個方法doInBackground,并且通常需要重 寫第二個方法onPostExecute異步任務的三個泛型參數,Params這個參數是當執行任務是發送給task的參數;Progress是在后臺計算 的進度回調返回的類型;Result是后臺線程計算的返回值。當一個異步任務執行的時候,task會經歷4步: 1. onPreExecute,task執行之前,在UI線程調用。這步通常用來設置task,例如用來在用戶的界面中展示一個進度條。 2. doInBackground,onPreExecute執行之后,立即在后臺線程調用。 這步是用來調用后臺計算,計算會花費較長時間。在這步中,異步任務的參數會傳遞進來。 計算的結果一定會在這步中返回回來,并且會傳遞到最后一步。 這步也會使用publishProgress用來發送一次或多次進度值。這些值會在主線程中通過onProgressUpdate這一步發布。 3. onProgressUpdate,在調用publishProgress之后在UI線程中調用onProgressUpdate。 執行的時機是不確定的。這個方法被用來在用戶界面展示任意來自于執行中的內容。 例如被用來驅動一個進度條或者使用文本來展示日志。 4. onPostExecute,當后臺線程執行完后在UI線程調用。后臺計算的結果會在這一步中通過參數形式傳遞。取消task 在任何時候都可以通過調用cancel(boolean)來取消task。 調用這個方法會造成,后續調用isCancelled()時返回true。 調用這個方法后,在執行完doInBackground(Object[])后,onCancelled(Object)會代替onPostExecute(Object)被調用。 確保任務盡可能快的被取消,比如在包含循環的情況下你應該經常定期在doInBackground(Object[])中檢查isCancelled()的返回值。Threading rules 在這個類中為了能確保正常工作,需要遵循一些線程規則:異步任務類必須要UI線程中進行加載。在android.os.Build.VERSION_CODES JELLY_BEAN 版本中自動完成了。task實例必須在UI線程中創建。execute方法一定要在UI線程中調用。不要手動調用onPreExecute、onPostExecute、doInBackground、onProgressUpdate。每一個task只能執行一次(如果當再次調用execution時,會拋出異常)。Memory observability AsyncTask通過該方法,下面操作在沒有明確同步操作下是安全的,來保證所有的callback調用是同步的。在構造方法或者onPreExecute設置成員字段,并在doInBackground中進行引用。通過doInBackground設置成員變量,在onProgressUpdate和onPostExecute中進行引用。Order of execution 當第一次對外公布時,AsyncTasks只會在一個后臺線程中串行執行。 自從android.os.Build.VERSION_CODES DONUT 1.6后,變為使用一種允許并行操作多個任務的線程池執行。 在android.os.Build.VERSION_CODES HONEYCOMB 3.0.x后,任務被執行在單一線程中來避免一些因為并行引起的程序錯誤。 如果你想真正的并行執行,那么你需要使用結合THREAD_POOL_EXECUTOR去調用executeOnExecutor(java.util.concurrent.Executor, Object[])。 復制代碼上面介紹了AsyncTask的使用方法,注意事項等。 下面是原文:
/*** <p>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.</p>** <p>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 <code>java.util.concurrent</code> package such as {@link Executor},* {@link ThreadPoolExecutor} and {@link FutureTask}.</p>** <p>An asynchronous task is defined by a computation that runs on a background thread and* whose result is published on the UI thread. An asynchronous task is defined by 3 generic* types, called <code>Params</code>, <code>Progress</code> and <code>Result</code>,* and 4 steps, called <code>onPreExecute</code>, <code>doInBackground</code>,* <code>onProgressUpdate</code> and <code>onPostExecute</code>.</p>** <div class="special reference">* <h3>Developer Guides</h3>* <p>For more information about using tasks and threads, read the* <a href="{@docRoot}guide/components/processes-and-threads.html">Processes and* Threads</a> developer guide.</p>* </div>** <h2>Usage</h2>* <p>AsyncTask must be subclassed to be used. The subclass will override at least* one method ({@link #doInBackground}), and most often will override a* second one ({@link #onPostExecute}.)</p>** <p>Here is an example of subclassing:</p>* <pre class="prettyprint">* 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");* }* }* </pre>** <p>Once created, a task is executed very simply:</p>* <pre class="prettyprint">* new DownloadFilesTask().execute(url1, url2, url3);* </pre>** <h2>AsyncTask's generic types</h2>* <p>The three types used by an asynchronous task are the following:</p>* <ol>* <li><code>Params</code>, the type of the parameters sent to the task upon* execution.</li>* <li><code>Progress</code>, the type of the progress units published during* the background computation.</li>* <li><code>Result</code>, the type of the result of the background* computation.</li>* </ol>* <p>Not all types are always used by an asynchronous task. To mark a type as unused,* simply use the type {@link Void}:</p>* <pre>* private class MyTask extends AsyncTask<Void, Void, Void> { ... }* </pre>** <h2>The 4 steps</h2>* <p>When an asynchronous task is executed, the task goes through 4 steps:</p>* <ol>* <li>{@link #onPreExecute()}, invoked on the UI thread before the task* is executed. This step is normally used to setup the task, for instance by* showing a progress bar in the user interface.</li>* <li>{@link #doInBackground}, invoked on the background thread* immediately after {@link #onPreExecute()} finishes executing. This step is used* to perform background computation that can take a long time. The parameters* of the asynchronous task are passed to this step. The result of the computation must* be returned by this step and will be passed back to the last step. This step* can also use {@link #publishProgress} to publish one or more units* of progress. These values are published on the UI thread, in the* {@link #onProgressUpdate} step.</li>* <li>{@link #onProgressUpdate}, invoked on the UI thread after a* call to {@link #publishProgress}. The timing of the execution is* undefined. This method is used to display any form of progress in the user* interface while the background computation is still executing. For instance,* it can be used to animate a progress bar or show logs in a text field.</li>* <li>{@link #onPostExecute}, invoked on the UI thread after the background* computation finishes. The result of the background computation is passed to* this step as a parameter.</li>* </ol>* * <h2>Cancelling a task</h2>* <p>A task can be cancelled at any time by invoking {@link #cancel(boolean)}. Invoking* this method will cause subsequent calls to {@link #isCancelled()} to return true.* After invoking this method, {@link #onCancelled(Object)}, instead of* {@link #onPostExecute(Object)} will be invoked after {@link #doInBackground(Object[])}* returns. To ensure that a task is cancelled as quickly as possible, you should always* check the return value of {@link #isCancelled()} periodically from* {@link #doInBackground(Object[])}, if possible (inside a loop for instance.)</p>** <h2>Threading rules</h2>* <p>There are a few threading rules that must be followed for this class to* work properly:</p>* <ul>* <li>The AsyncTask class must be loaded on the UI thread. This is done* automatically as of {@link android.os.Build.VERSION_CODES#JELLY_BEAN}.</li>* <li>The task instance must be created on the UI thread.</li>* <li>{@link #execute} must be invoked on the UI thread.</li>* <li>Do not call {@link #onPreExecute()}, {@link #onPostExecute},* {@link #doInBackground}, {@link #onProgressUpdate} manually.</li>* <li>The task can be executed only once (an exception will be thrown if* a second execution is attempted.)</li>* </ul>** <h2>Memory observability</h2>* <p>AsyncTask guarantees that all callback calls are synchronized in such a way that the following* operations are safe without explicit synchronizations.</p>* <ul>* <li>Set member fields in the constructor or {@link #onPreExecute}, and refer to them* in {@link #doInBackground}.* <li>Set member fields in {@link #doInBackground}, and refer to them in* {@link #onProgressUpdate} and {@link #onPostExecute}.* </ul>** <h2>Order of execution</h2>* <p>When first introduced, AsyncTasks were executed serially on a single background* thread. Starting with {@link android.os.Build.VERSION_CODES#DONUT}, this was changed* to a pool of threads allowing multiple tasks to operate in parallel. Starting with* {@link android.os.Build.VERSION_CODES#HONEYCOMB}, tasks are executed on a single* thread to avoid common application errors caused by parallel execution.</p>* <p>If you truly want parallel execution, you can invoke* {@link #executeOnExecutor(java.util.concurrent.Executor, Object[])} with* {@link #THREAD_POOL_EXECUTOR}.</p>*/ 復制代碼下面進行源碼分析: 還是老規矩,通過成員變量入手,
//cpu可用數量 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 work // 我們想要在線程池中至少有2個線程和最多4個線程,寧愿比cpu數量少1,以此來避免因為后臺工作引起的飽和。//保存在池中的線程數 private 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; //當線程數大于cpu核數的時候,KEEP_ALIVE_SECONDS該時間為空余線程在終止之前等待新任務的最大時間。 private static final int KEEP_ALIVE_SECONDS = 30;//用來創建新線程 private static final ThreadFactory sThreadFactory = new ThreadFactory() {private final AtomicInteger mCount = new AtomicInteger(1);public Thread newThread(Runnable r) {return new Thread(r, "AsyncTask #" + mCount.getAndIncrement());} };//在task被執行之前,存儲在sPoolWorkQueue 隊列中。該隊列進存放通過execute方法添加的Runnable任務。 private static final BlockingQueue<Runnable> sPoolWorkQueue =new LinkedBlockingQueue<Runnable>(128);/*** An {@link Executor} that can be used to execute tasks in parallel.*/ public static final Executor THREAD_POOL_EXECUTOR;static {//上面都是準備工作,準備構造ThreadPoolExecutor。ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_SECONDS, TimeUnit.SECONDS,sPoolWorkQueue, sThreadFactory);//線程池數量最后銷毀到0個threadPoolExecutor.allowCoreThreadTimeOut(true);THREAD_POOL_EXECUTOR = threadPoolExecutor; }/*** An {@link Executor} that executes tasks one at a time in serial* order. This serialization is global to a particular process.*/ //SERIAL_EXECUTOR 是順序執行task的。在指定進程中是全局的。 public static final Executor SERIAL_EXECUTOR = new SerialExecutor();private static final int MESSAGE_POST_RESULT = 0x1; private static final int MESSAGE_POST_PROGRESS = 0x2;//執行task的Executor,默認為SerialExecutor,后面介紹SerialExecutor類 private static volatile Executor sDefaultExecutor = SERIAL_EXECUTOR;//通過sHandler將后臺線程返回的結果轉到主線程 private static InternalHandler sHandler;//mWorker在AsyncTask的構造方法中創建 private final WorkerRunnable<Params, Result> mWorker; private final FutureTask<Result> mFuture;private volatile Status mStatus = Status.PENDING;//用來同步線程是否被取消,或者執行失敗 private final AtomicBoolean mCancelled = new AtomicBoolean(); //用來同步task是否被調用 private final AtomicBoolean mTaskInvoked = new AtomicBoolean();//Executor, 默認的Executor,按順序一個個執行。在制定進程中是全局的。 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) {//如果mTasks 中沒有任務執行那么將任務通過THREAD_POOL_EXECUTOR執行。scheduleNext();}}protected synchronized void scheduleNext() {if ((mActive = mTasks.poll()) != null) {//通過THREAD_POOL_EXECUTOR執行THREAD_POOL_EXECUTOR.execute(mActive);}} }/*** Indicates the current status of the task. Each status will be set only once* during the lifetime of a task.*///表示當前任務狀態。在task的生命周期內每一個狀態僅能被設置一次。 public enum Status {/*** Indicates that the task has not been executed yet.*///標示task還沒有被執行PENDING,/*** Indicates that the task is running.*///task正在執行RUNNING,/*** Indicates that {@link AsyncTask#onPostExecute} has finished.*///onPostExecute已經執行完成。FINISHED, }/** @hide */ //更換sDefaultExecutor ,為hide方法,默認不對開發者提供,默認使用SerialExecutor public static void setDefaultExecutor(Executor exec) {sDefaultExecutor = exec; }/*** Creates a new asynchronous task. This constructor must be invoked on the UI thread.*///創建一個新的異步任務。該構造方法一定要在UI線程調用。 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//傳遞mParams,并執行doInBackground()result = doInBackground(mParams);//官方解釋:flush在當前線程任何等待的Binder命令到內核驅動程序。調用這個函數是有益的//在執行一個可能阻塞很長時間的操作。為了確保任何等待的object引用會被釋放,//為了防止進程對對象持有超過它需要的時間//翻譯的不是很好, 查閱了一些資料//Binder.flushPendingCommands()方法被調用說明后面的代碼可能會引起線程阻塞Binder.flushPendingCommands();} catch (Throwable tr) {//當有異常時,設置mCancelled為true,說明被取消mCancelled.set(true);throw tr;} finally {//通過主線程返回結果postResult(result);}return result;}};//mWorker 會放在FutureTask中執行。mFuture = new FutureTask<Result>(mWorker) {@Overrideprotected void done() {//如果task沒有調用,通過postResultIfNotInvoked,返回結果try {//get()會引起阻塞,等待計算結果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);}}}; } private void postResultIfNotInvoked(Result result) {final boolean wasTaskInvoked = mTaskInvoked.get();//如果task沒有調用,通過postResultIfNotInvoked,返回結果if (!wasTaskInvoked) {postResult(result);} } private Result postResult(Result result) {//最終通過該方法返回結果,無論task是否執行。//getHandler會在主線中執行MESSAGE_POST_RESULT case 返回result@SuppressWarnings("unchecked")Message message = getHandler().obtainMessage(MESSAGE_POST_RESULT,new AsyncTaskResult<Result>(this, result));message.sendToTarget();return result; }private static class InternalHandler extends Handler {public InternalHandler() {//綁定到主線程的Loopersuper(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// 在主線程執行finish()result.mTask.finish(result.mData[0]);break;case MESSAGE_POST_PROGRESS:// 在主線程回調onProgressUpdateresult.mTask.onProgressUpdate(result.mData);break;}} }private void finish(Result result) {if (isCancelled()) {//如果task被取消,調用onCancelled(),不會調用onPostExecuteonCancelled(result);} else {onPostExecute(result);}//更新task狀態mStatus = Status.FINISHED; } 復制代碼下面介紹一下幾個子類需要復寫的方法
/*** Override this method to perform a computation on a background thread. The* specified parameters are the parameters passed to {@link #execute}* by the caller of this task.** This method can call {@link #publishProgress} to publish updates* on the UI thread.** @param params The parameters of the task.** @return A result, defined by the subclass of this task.** @see #onPreExecute()* @see #onPostExecute* @see #publishProgress*/// 復寫該方法,在后臺線程中執行計算。通過調用execute傳遞的指定的參數 params// 該方法可以在主線程調用publishProgress來更新進度// 返回一個result。類型由task子類定義的類型。 @WorkerThread protected abstract Result doInBackground(Params... params);/*** Runs on the UI thread before {@link #doInBackground}.** @see #onPostExecute* @see #doInBackground*/// 在調用doInBackground之前,首先在主線程調用onPreExecute// 在子線程計算之前,需要在主線程準備一些工作。就復寫該方法在這里面完成 @MainThread protected void onPreExecute() { }/*** <p>Runs on the UI thread after {@link #doInBackground}. The* specified result is the value returned by {@link #doInBackground}.</p>* * <p>This method won't be invoked if the task was cancelled.</p>** @param result The result of the operation computed by {@link #doInBackground}.** @see #onPreExecute* @see #doInBackground* @see #onCancelled(Object) */// 當doInBackground執行完成后,會在主線程中調用onPostExecute方法。參數result是// doInBackground返回的。// 如果task被取消了,那么就不會調用onPostExecute返回結果。而是通過onCancelled @SuppressWarnings({"UnusedDeclaration"}) @MainThread protected void onPostExecute(Result result) { }/*** Runs on the UI thread after {@link #publishProgress} is invoked.* The specified values are the values passed to {@link #publishProgress}.** @param values The values indicating progress.** @see #publishProgress* @see #doInBackground*/// 調用publishProgress之后,在主線程調用onProgressUpdate。// values參數是publishProgress傳遞過來的。 @SuppressWarnings({"UnusedDeclaration"}) @MainThread protected void onProgressUpdate(Progress... values) { }/*** <p>Runs on the UI thread after {@link #cancel(boolean)} is invoked and* {@link #doInBackground(Object[])} has finished.</p>* * <p>The default implementation simply invokes {@link #onCancelled()} and* ignores the result. If you write your own implementation, do not call* <code>super.onCancelled(result)</code>.</p>** @param result The result, if any, computed in* {@link #doInBackground(Object[])}, can be null* * @see #cancel(boolean)* @see #isCancelled()*/// cancel調用之后,會在主線程調用onCancelled方法。并且doInBackground已經執行完成。// 默認實現簡單的調用調用了onCancelled(),并且忽略result。// 如果寫了自己的實現,那么就不需要調用super.onCancelled(result)。// result 為doInBackground計算的結果,可能為null @SuppressWarnings({"UnusedParameters"}) @MainThread protected void onCancelled(Result result) {onCancelled(); } /*** <p>Applications should preferably override {@link #onCancelled(Object)}.* This method is invoked by the default implementation of* {@link #onCancelled(Object)}.</p>* * <p>Runs on the UI thread after {@link #cancel(boolean)} is invoked and* {@link #doInBackground(Object[])} has finished.</p>** @see #onCancelled(Object) * @see #cancel(boolean)* @see #isCancelled()*/// 最好復寫onCancelled(object)方法。這個方法被默認實現的onCancelled(Object)方法所調用。// 在cancel(boolean)運行之后并且doInBackground(Object[])也調用完成之后,在主線程調用onCancelled() @MainThread protected void onCancelled() { } 復制代碼大概分析到這。
總結一下:
Thanks.
轉載于:https://juejin.im/post/5ae841a36fb9a07ac36341d6
總結
以上是生活随笔為你收集整理的小葵花妈妈课堂开课了:《AsyncTask源码分析》的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Arduino UNO驱动DS1307数
- 下一篇: python counter转换为列表_