The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.os; |
| 18 | |
Tor Norbye | 83c6896 | 2015-03-10 20:55:31 -0700 | [diff] [blame] | 19 | import android.annotation.MainThread; |
Makoto Onuki | 1488a3a | 2017-05-24 12:25:46 -0700 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Tor Norbye | 83c6896 | 2015-03-10 20:55:31 -0700 | [diff] [blame] | 21 | import android.annotation.WorkerThread; |
Artur Satayev | afdb23a | 2019-12-10 17:47:53 +0000 | [diff] [blame] | 22 | import android.compat.annotation.UnsupportedAppUsage; |
Hans Boehm | 13060ff | 2019-01-31 17:22:55 -0800 | [diff] [blame] | 23 | |
Joe Onorato | 81de61b | 2011-01-16 13:04:51 -0800 | [diff] [blame] | 24 | import java.util.ArrayDeque; |
Gilles Debunne | 7f4b684 | 2010-04-27 17:54:21 -0700 | [diff] [blame] | 25 | import java.util.concurrent.Callable; |
| 26 | import java.util.concurrent.CancellationException; |
| 27 | import java.util.concurrent.ExecutionException; |
Makoto Onuki | 1488a3a | 2017-05-24 12:25:46 -0700 | [diff] [blame] | 28 | import java.util.concurrent.Executor; |
Gilles Debunne | 7f4b684 | 2010-04-27 17:54:21 -0700 | [diff] [blame] | 29 | import java.util.concurrent.FutureTask; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import java.util.concurrent.LinkedBlockingQueue; |
Hans Boehm | 573bb79 | 2019-02-01 17:52:40 -0800 | [diff] [blame] | 31 | import java.util.concurrent.RejectedExecutionHandler; |
| 32 | import java.util.concurrent.SynchronousQueue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import java.util.concurrent.ThreadFactory; |
Gilles Debunne | 7f4b684 | 2010-04-27 17:54:21 -0700 | [diff] [blame] | 34 | import java.util.concurrent.ThreadPoolExecutor; |
| 35 | import java.util.concurrent.TimeUnit; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import java.util.concurrent.TimeoutException; |
Romain Guy | 5ba812b | 2011-01-10 13:33:12 -0800 | [diff] [blame] | 37 | import java.util.concurrent.atomic.AtomicBoolean; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import java.util.concurrent.atomic.AtomicInteger; |
| 39 | |
| 40 | /** |
Charles Munger | 0a96c3b | 2019-10-31 23:17:42 +0000 | [diff] [blame] | 41 | * <p>AsyncTask was intended to enable proper and easy use of the UI thread. However, the most |
| 42 | * common use case was for integrating into UI, and that would cause Context leaks, missed |
| 43 | * callbacks, or crashes on configuration changes. It also has inconsistent behavior on different |
| 44 | * versions of the platform, swallows exceptions from {@code doInBackground}, and does not provide |
| 45 | * much utility over using {@link Executor}s directly.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | * |
Romain Guy | 2c1b8c7 | 2012-05-21 10:43:26 -0700 | [diff] [blame] | 47 | * <p>AsyncTask is designed to be a helper class around {@link Thread} and {@link Handler} |
| 48 | * and does not constitute a generic threading framework. AsyncTasks should ideally be |
| 49 | * used for short operations (a few seconds at the most.) If you need to keep threads |
| 50 | * running for long periods of time, it is highly recommended you use the various APIs |
Patrick Tjin | 72fa3ed | 2014-02-25 14:52:23 -0800 | [diff] [blame] | 51 | * provided by the <code>java.util.concurrent</code> package such as {@link Executor}, |
Romain Guy | 2c1b8c7 | 2012-05-21 10:43:26 -0700 | [diff] [blame] | 52 | * {@link ThreadPoolExecutor} and {@link FutureTask}.</p> |
| 53 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | * <p>An asynchronous task is defined by a computation that runs on a background thread and |
Romain Guy | 0bbd8d8 | 2011-10-11 18:13:37 -0700 | [diff] [blame] | 55 | * whose result is published on the UI thread. An asynchronous task is defined by 3 generic |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | * types, called <code>Params</code>, <code>Progress</code> and <code>Result</code>, |
Gilles Debunne | 7f4b684 | 2010-04-27 17:54:21 -0700 | [diff] [blame] | 57 | * and 4 steps, called <code>onPreExecute</code>, <code>doInBackground</code>, |
| 58 | * <code>onProgressUpdate</code> and <code>onPostExecute</code>.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | * |
Joe Fernandez | b54e7a3 | 2011-10-03 15:09:50 -0700 | [diff] [blame] | 60 | * <div class="special reference"> |
| 61 | * <h3>Developer Guides</h3> |
| 62 | * <p>For more information about using tasks and threads, read the |
Mark Lu | 53415ff | 2016-08-12 13:02:32 -0700 | [diff] [blame] | 63 | * <a href="{@docRoot}guide/components/processes-and-threads.html">Processes and |
Joe Fernandez | b54e7a3 | 2011-10-03 15:09:50 -0700 | [diff] [blame] | 64 | * Threads</a> developer guide.</p> |
| 65 | * </div> |
| 66 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | * <h2>Usage</h2> |
| 68 | * <p>AsyncTask must be subclassed to be used. The subclass will override at least |
| 69 | * one method ({@link #doInBackground}), and most often will override a |
| 70 | * second one ({@link #onPostExecute}.)</p> |
| 71 | * |
| 72 | * <p>Here is an example of subclassing:</p> |
| 73 | * <pre class="prettyprint"> |
| 74 | * private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { |
| 75 | * protected Long doInBackground(URL... urls) { |
| 76 | * int count = urls.length; |
| 77 | * long totalSize = 0; |
Hans Boehm | 13060ff | 2019-01-31 17:22:55 -0800 | [diff] [blame] | 78 | * for (int i = 0; i < count; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | * totalSize += Downloader.downloadFile(urls[i]); |
| 80 | * publishProgress((int) ((i / (float) count) * 100)); |
Romain Guy | 2c1b8c7 | 2012-05-21 10:43:26 -0700 | [diff] [blame] | 81 | * // Escape early if cancel() is called |
| 82 | * if (isCancelled()) break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | * } |
| 84 | * return totalSize; |
| 85 | * } |
| 86 | * |
| 87 | * protected void onProgressUpdate(Integer... progress) { |
| 88 | * setProgressPercent(progress[0]); |
| 89 | * } |
| 90 | * |
| 91 | * protected void onPostExecute(Long result) { |
| 92 | * showDialog("Downloaded " + result + " bytes"); |
| 93 | * } |
| 94 | * } |
| 95 | * </pre> |
| 96 | * |
| 97 | * <p>Once created, a task is executed very simply:</p> |
| 98 | * <pre class="prettyprint"> |
| 99 | * new DownloadFilesTask().execute(url1, url2, url3); |
| 100 | * </pre> |
| 101 | * |
| 102 | * <h2>AsyncTask's generic types</h2> |
| 103 | * <p>The three types used by an asynchronous task are the following:</p> |
| 104 | * <ol> |
| 105 | * <li><code>Params</code>, the type of the parameters sent to the task upon |
| 106 | * execution.</li> |
| 107 | * <li><code>Progress</code>, the type of the progress units published during |
| 108 | * the background computation.</li> |
| 109 | * <li><code>Result</code>, the type of the result of the background |
| 110 | * computation.</li> |
| 111 | * </ol> |
Owen Lin | 202f560 | 2009-12-28 15:54:23 +0800 | [diff] [blame] | 112 | * <p>Not all types are always used by an asynchronous task. To mark a type as unused, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | * simply use the type {@link Void}:</p> |
| 114 | * <pre> |
Romain Guy | 6a1ae64 | 2009-05-02 22:52:17 -0700 | [diff] [blame] | 115 | * private class MyTask extends AsyncTask<Void, Void, Void> { ... } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | * </pre> |
| 117 | * |
| 118 | * <h2>The 4 steps</h2> |
| 119 | * <p>When an asynchronous task is executed, the task goes through 4 steps:</p> |
| 120 | * <ol> |
Romain Guy | 6f931cc | 2012-10-17 11:06:11 -0700 | [diff] [blame] | 121 | * <li>{@link #onPreExecute()}, invoked on the UI thread before the task |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | * is executed. This step is normally used to setup the task, for instance by |
| 123 | * showing a progress bar in the user interface.</li> |
| 124 | * <li>{@link #doInBackground}, invoked on the background thread |
| 125 | * immediately after {@link #onPreExecute()} finishes executing. This step is used |
| 126 | * to perform background computation that can take a long time. The parameters |
| 127 | * of the asynchronous task are passed to this step. The result of the computation must |
| 128 | * be returned by this step and will be passed back to the last step. This step |
| 129 | * can also use {@link #publishProgress} to publish one or more units |
Romain Guy | 0bbd8d8 | 2011-10-11 18:13:37 -0700 | [diff] [blame] | 130 | * of progress. These values are published on the UI thread, in the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | * {@link #onProgressUpdate} step.</li> |
Romain Guy | 0bbd8d8 | 2011-10-11 18:13:37 -0700 | [diff] [blame] | 132 | * <li>{@link #onProgressUpdate}, invoked on the UI thread after a |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | * call to {@link #publishProgress}. The timing of the execution is |
| 134 | * undefined. This method is used to display any form of progress in the user |
| 135 | * interface while the background computation is still executing. For instance, |
| 136 | * it can be used to animate a progress bar or show logs in a text field.</li> |
Romain Guy | 0bbd8d8 | 2011-10-11 18:13:37 -0700 | [diff] [blame] | 137 | * <li>{@link #onPostExecute}, invoked on the UI thread after the background |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | * computation finishes. The result of the background computation is passed to |
| 139 | * this step as a parameter.</li> |
| 140 | * </ol> |
Romain Guy | e95003e | 2011-01-09 13:53:06 -0800 | [diff] [blame] | 141 | * |
| 142 | * <h2>Cancelling a task</h2> |
| 143 | * <p>A task can be cancelled at any time by invoking {@link #cancel(boolean)}. Invoking |
| 144 | * this method will cause subsequent calls to {@link #isCancelled()} to return true. |
Romain Guy | 5ba812b | 2011-01-10 13:33:12 -0800 | [diff] [blame] | 145 | * After invoking this method, {@link #onCancelled(Object)}, instead of |
| 146 | * {@link #onPostExecute(Object)} will be invoked after {@link #doInBackground(Object[])} |
| 147 | * returns. To ensure that a task is cancelled as quickly as possible, you should always |
| 148 | * check the return value of {@link #isCancelled()} periodically from |
| 149 | * {@link #doInBackground(Object[])}, if possible (inside a loop for instance.)</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | * |
| 151 | * <h2>Threading rules</h2> |
| 152 | * <p>There are a few threading rules that must be followed for this class to |
| 153 | * work properly:</p> |
| 154 | * <ul> |
Romain Guy | 5e9120d | 2012-01-30 12:17:22 -0800 | [diff] [blame] | 155 | * <li>The AsyncTask class must be loaded on the UI thread. This is done |
| 156 | * automatically as of {@link android.os.Build.VERSION_CODES#JELLY_BEAN}.</li> |
Romain Guy | 0bbd8d8 | 2011-10-11 18:13:37 -0700 | [diff] [blame] | 157 | * <li>The task instance must be created on the UI thread.</li> |
| 158 | * <li>{@link #execute} must be invoked on the UI thread.</li> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | * <li>Do not call {@link #onPreExecute()}, {@link #onPostExecute}, |
| 160 | * {@link #doInBackground}, {@link #onProgressUpdate} manually.</li> |
| 161 | * <li>The task can be executed only once (an exception will be thrown if |
| 162 | * a second execution is attempted.)</li> |
| 163 | * </ul> |
Makoto Onuki | aa60a02 | 2010-08-31 09:42:12 -0700 | [diff] [blame] | 164 | * |
| 165 | * <h2>Memory observability</h2> |
Hans Boehm | 13060ff | 2019-01-31 17:22:55 -0800 | [diff] [blame] | 166 | * <p>AsyncTask guarantees that all callback calls are synchronized to ensure the following |
| 167 | * without explicit synchronizations.</p> |
Makoto Onuki | aa60a02 | 2010-08-31 09:42:12 -0700 | [diff] [blame] | 168 | * <ul> |
Hans Boehm | 13060ff | 2019-01-31 17:22:55 -0800 | [diff] [blame] | 169 | * <li>The memory effects of {@link #onPreExecute}, and anything else |
| 170 | * executed before the call to {@link #execute}, including the construction |
| 171 | * of the AsyncTask object, are visible to {@link #doInBackground}. |
| 172 | * <li>The memory effects of {@link #doInBackground} are visible to |
| 173 | * {@link #onPostExecute}. |
| 174 | * <li>Any memory effects of {@link #doInBackground} preceding a call |
| 175 | * to {@link #publishProgress} are visible to the corresponding |
| 176 | * {@link #onProgressUpdate} call. (But {@link #doInBackground} continues to |
| 177 | * run, and care needs to be taken that later updates in {@link #doInBackground} |
| 178 | * do not interfere with an in-progress {@link #onProgressUpdate} call.) |
| 179 | * <li>Any memory effects preceding a call to {@link #cancel} are visible |
| 180 | * after a call to {@link #isCancelled} that returns true as a result, or |
| 181 | * during and after a resulting call to {@link #onCancelled}. |
Makoto Onuki | aa60a02 | 2010-08-31 09:42:12 -0700 | [diff] [blame] | 182 | * </ul> |
Romain Guy | 2c1b8c7 | 2012-05-21 10:43:26 -0700 | [diff] [blame] | 183 | * |
| 184 | * <h2>Order of execution</h2> |
| 185 | * <p>When first introduced, AsyncTasks were executed serially on a single background |
| 186 | * thread. Starting with {@link android.os.Build.VERSION_CODES#DONUT}, this was changed |
| 187 | * to a pool of threads allowing multiple tasks to operate in parallel. Starting with |
| 188 | * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, tasks are executed on a single |
| 189 | * thread to avoid common application errors caused by parallel execution.</p> |
| 190 | * <p>If you truly want parallel execution, you can invoke |
| 191 | * {@link #executeOnExecutor(java.util.concurrent.Executor, Object[])} with |
| 192 | * {@link #THREAD_POOL_EXECUTOR}.</p> |
Charles Munger | 0a96c3b | 2019-10-31 23:17:42 +0000 | [diff] [blame] | 193 | * |
| 194 | * @deprecated Use the standard <code>java.util.concurrent</code> or |
| 195 | * <a href="https://developer.android.com/topic/libraries/architecture/coroutines"> |
| 196 | * Kotlin concurrency utilities</a> instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | */ |
Charles Munger | 0a96c3b | 2019-10-31 23:17:42 +0000 | [diff] [blame] | 198 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | public abstract class AsyncTask<Params, Progress, Result> { |
| 200 | private static final String LOG_TAG = "AsyncTask"; |
| 201 | |
Hans Boehm | 573bb79 | 2019-02-01 17:52:40 -0800 | [diff] [blame] | 202 | // We keep only a single pool thread around all the time. |
| 203 | // We let the pool grow to a fairly large number of threads if necessary, |
| 204 | // but let them time out quickly. In the unlikely case that we run out of threads, |
| 205 | // we fall back to a simple unbounded-queue executor. |
| 206 | // This combination ensures that: |
| 207 | // 1. We normally keep few threads (1) around. |
| 208 | // 2. We queue only after launching a significantly larger, but still bounded, set of threads. |
| 209 | // 3. We keep the total number of threads bounded, but still allow an unbounded set |
| 210 | // of tasks to be queued. |
| 211 | private static final int CORE_POOL_SIZE = 1; |
| 212 | private static final int MAXIMUM_POOL_SIZE = 20; |
| 213 | private static final int BACKUP_POOL_SIZE = 5; |
| 214 | private static final int KEEP_ALIVE_SECONDS = 3; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | private static final ThreadFactory sThreadFactory = new ThreadFactory() { |
| 217 | private final AtomicInteger mCount = new AtomicInteger(1); |
| 218 | |
| 219 | public Thread newThread(Runnable r) { |
| 220 | return new Thread(r, "AsyncTask #" + mCount.getAndIncrement()); |
| 221 | } |
| 222 | }; |
| 223 | |
Hans Boehm | 573bb79 | 2019-02-01 17:52:40 -0800 | [diff] [blame] | 224 | // Used only for rejected executions. |
| 225 | // Initialization protected by sRunOnSerialPolicy lock. |
| 226 | private static ThreadPoolExecutor sBackupExecutor; |
| 227 | private static LinkedBlockingQueue<Runnable> sBackupExecutorQueue; |
| 228 | |
| 229 | private static final RejectedExecutionHandler sRunOnSerialPolicy = |
| 230 | new RejectedExecutionHandler() { |
| 231 | public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { |
| 232 | android.util.Log.w(LOG_TAG, "Exceeded ThreadPoolExecutor pool size"); |
| 233 | // As a last ditch fallback, run it on an executor with an unbounded queue. |
| 234 | // Create this executor lazily, hopefully almost never. |
| 235 | synchronized (this) { |
| 236 | if (sBackupExecutor == null) { |
| 237 | sBackupExecutorQueue = new LinkedBlockingQueue<Runnable>(); |
| 238 | sBackupExecutor = new ThreadPoolExecutor( |
| 239 | BACKUP_POOL_SIZE, BACKUP_POOL_SIZE, KEEP_ALIVE_SECONDS, |
| 240 | TimeUnit.SECONDS, sBackupExecutorQueue, sThreadFactory); |
| 241 | sBackupExecutor.allowCoreThreadTimeOut(true); |
| 242 | } |
| 243 | } |
| 244 | sBackupExecutor.execute(r); |
| 245 | } |
| 246 | }; |
Joe Onorato | 81de61b | 2011-01-16 13:04:51 -0800 | [diff] [blame] | 247 | |
| 248 | /** |
Dianne Hackborn | 96438cd | 2011-01-25 21:42:37 -0800 | [diff] [blame] | 249 | * An {@link Executor} that can be used to execute tasks in parallel. |
Charles Munger | 0a96c3b | 2019-10-31 23:17:42 +0000 | [diff] [blame] | 250 | * |
| 251 | * @deprecated Using a single thread pool for a general purpose results in suboptimal behavior |
| 252 | * for different tasks. Small, CPU-bound tasks benefit from a bounded pool and queueing, and |
| 253 | * long-running blocking tasks, such as network operations, benefit from many threads. Use or |
| 254 | * create an {@link Executor} configured for your use case. |
Joe Onorato | 81de61b | 2011-01-16 13:04:51 -0800 | [diff] [blame] | 255 | */ |
Charles Munger | 0a96c3b | 2019-10-31 23:17:42 +0000 | [diff] [blame] | 256 | @Deprecated |
John Reck | 2b0ebb3 | 2015-11-30 15:45:25 -0800 | [diff] [blame] | 257 | public static final Executor THREAD_POOL_EXECUTOR; |
| 258 | |
| 259 | static { |
| 260 | ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor( |
| 261 | CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_SECONDS, TimeUnit.SECONDS, |
Hans Boehm | 573bb79 | 2019-02-01 17:52:40 -0800 | [diff] [blame] | 262 | new SynchronousQueue<Runnable>(), sThreadFactory); |
| 263 | threadPoolExecutor.setRejectedExecutionHandler(sRunOnSerialPolicy); |
John Reck | 2b0ebb3 | 2015-11-30 15:45:25 -0800 | [diff] [blame] | 264 | THREAD_POOL_EXECUTOR = threadPoolExecutor; |
| 265 | } |
Joe Onorato | 81de61b | 2011-01-16 13:04:51 -0800 | [diff] [blame] | 266 | |
Dianne Hackborn | 96438cd | 2011-01-25 21:42:37 -0800 | [diff] [blame] | 267 | /** |
| 268 | * An {@link Executor} that executes tasks one at a time in serial |
| 269 | * order. This serialization is global to a particular process. |
Charles Munger | 0a96c3b | 2019-10-31 23:17:42 +0000 | [diff] [blame] | 270 | * |
| 271 | * @deprecated Globally serializing tasks results in excessive queuing for unrelated operations. |
Dianne Hackborn | 96438cd | 2011-01-25 21:42:37 -0800 | [diff] [blame] | 272 | */ |
Charles Munger | 0a96c3b | 2019-10-31 23:17:42 +0000 | [diff] [blame] | 273 | @Deprecated |
Dianne Hackborn | 96438cd | 2011-01-25 21:42:37 -0800 | [diff] [blame] | 274 | public static final Executor SERIAL_EXECUTOR = new SerialExecutor(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | |
| 276 | private static final int MESSAGE_POST_RESULT = 0x1; |
| 277 | private static final int MESSAGE_POST_PROGRESS = 0x2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | |
Andrei Onea | dcb6773 | 2019-03-18 11:37:25 +0000 | [diff] [blame] | 279 | @UnsupportedAppUsage |
Joe Onorato | d630f10 | 2011-03-17 18:42:26 -0700 | [diff] [blame] | 280 | private static volatile Executor sDefaultExecutor = SERIAL_EXECUTOR; |
Jeff Brown | bba231d | 2014-11-14 15:49:45 -0800 | [diff] [blame] | 281 | private static InternalHandler sHandler; |
| 282 | |
Andrei Onea | dcb6773 | 2019-03-18 11:37:25 +0000 | [diff] [blame] | 283 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | private final WorkerRunnable<Params, Result> mWorker; |
Andrei Onea | dcb6773 | 2019-03-18 11:37:25 +0000 | [diff] [blame] | 285 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | private final FutureTask<Result> mFuture; |
| 287 | |
Andrei Onea | dcb6773 | 2019-03-18 11:37:25 +0000 | [diff] [blame] | 288 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | private volatile Status mStatus = Status.PENDING; |
Romain Guy | 5ba812b | 2011-01-10 13:33:12 -0800 | [diff] [blame] | 290 | |
Romain Guy | 657f513 | 2011-12-05 14:49:33 -0800 | [diff] [blame] | 291 | private final AtomicBoolean mCancelled = new AtomicBoolean(); |
Andrei Onea | dcb6773 | 2019-03-18 11:37:25 +0000 | [diff] [blame] | 292 | @UnsupportedAppUsage |
Romain Guy | 5ba812b | 2011-01-10 13:33:12 -0800 | [diff] [blame] | 293 | private final AtomicBoolean mTaskInvoked = new AtomicBoolean(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | |
Makoto Onuki | 1488a3a | 2017-05-24 12:25:46 -0700 | [diff] [blame] | 295 | private final Handler mHandler; |
| 296 | |
Joe Onorato | 81de61b | 2011-01-16 13:04:51 -0800 | [diff] [blame] | 297 | private static class SerialExecutor implements Executor { |
| 298 | final ArrayDeque<Runnable> mTasks = new ArrayDeque<Runnable>(); |
| 299 | Runnable mActive; |
| 300 | |
| 301 | public synchronized void execute(final Runnable r) { |
| 302 | mTasks.offer(new Runnable() { |
| 303 | public void run() { |
| 304 | try { |
| 305 | r.run(); |
| 306 | } finally { |
| 307 | scheduleNext(); |
| 308 | } |
| 309 | } |
| 310 | }); |
| 311 | if (mActive == null) { |
| 312 | scheduleNext(); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | protected synchronized void scheduleNext() { |
| 317 | if ((mActive = mTasks.poll()) != null) { |
| 318 | THREAD_POOL_EXECUTOR.execute(mActive); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | /** |
| 324 | * Indicates the current status of the task. Each status will be set only once |
| 325 | * during the lifetime of a task. |
| 326 | */ |
| 327 | public enum Status { |
| 328 | /** |
| 329 | * Indicates that the task has not been executed yet. |
| 330 | */ |
| 331 | PENDING, |
| 332 | /** |
| 333 | * Indicates that the task is running. |
| 334 | */ |
| 335 | RUNNING, |
| 336 | /** |
| 337 | * Indicates that {@link AsyncTask#onPostExecute} has finished. |
| 338 | */ |
| 339 | FINISHED, |
| 340 | } |
| 341 | |
Makoto Onuki | 1488a3a | 2017-05-24 12:25:46 -0700 | [diff] [blame] | 342 | private static Handler getMainHandler() { |
Jeff Brown | bba231d | 2014-11-14 15:49:45 -0800 | [diff] [blame] | 343 | synchronized (AsyncTask.class) { |
| 344 | if (sHandler == null) { |
Makoto Onuki | 1488a3a | 2017-05-24 12:25:46 -0700 | [diff] [blame] | 345 | sHandler = new InternalHandler(Looper.getMainLooper()); |
Jeff Brown | bba231d | 2014-11-14 15:49:45 -0800 | [diff] [blame] | 346 | } |
| 347 | return sHandler; |
| 348 | } |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Makoto Onuki | 1488a3a | 2017-05-24 12:25:46 -0700 | [diff] [blame] | 351 | private Handler getHandler() { |
| 352 | return mHandler; |
| 353 | } |
| 354 | |
Joe Onorato | d630f10 | 2011-03-17 18:42:26 -0700 | [diff] [blame] | 355 | /** @hide */ |
Andrei Onea | dcb6773 | 2019-03-18 11:37:25 +0000 | [diff] [blame] | 356 | @UnsupportedAppUsage |
Joe Onorato | d630f10 | 2011-03-17 18:42:26 -0700 | [diff] [blame] | 357 | public static void setDefaultExecutor(Executor exec) { |
| 358 | sDefaultExecutor = exec; |
| 359 | } |
| 360 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | /** |
Romain Guy | 0bbd8d8 | 2011-10-11 18:13:37 -0700 | [diff] [blame] | 362 | * Creates a new asynchronous task. This constructor must be invoked on the UI thread. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | */ |
| 364 | public AsyncTask() { |
Makoto Onuki | 1488a3a | 2017-05-24 12:25:46 -0700 | [diff] [blame] | 365 | this((Looper) null); |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Creates a new asynchronous task. This constructor must be invoked on the UI thread. |
| 370 | * |
| 371 | * @hide |
| 372 | */ |
| 373 | public AsyncTask(@Nullable Handler handler) { |
| 374 | this(handler != null ? handler.getLooper() : null); |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Creates a new asynchronous task. This constructor must be invoked on the UI thread. |
| 379 | * |
| 380 | * @hide |
| 381 | */ |
| 382 | public AsyncTask(@Nullable Looper callbackLooper) { |
| 383 | mHandler = callbackLooper == null || callbackLooper == Looper.getMainLooper() |
| 384 | ? getMainHandler() |
| 385 | : new Handler(callbackLooper); |
| 386 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | mWorker = new WorkerRunnable<Params, Result>() { |
| 388 | public Result call() throws Exception { |
Romain Guy | 5ba812b | 2011-01-10 13:33:12 -0800 | [diff] [blame] | 389 | mTaskInvoked.set(true); |
Tony Mantler | 78a8e9d | 2016-08-05 14:08:06 -0700 | [diff] [blame] | 390 | Result result = null; |
| 391 | try { |
| 392 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 393 | //noinspection unchecked |
| 394 | result = doInBackground(mParams); |
| 395 | Binder.flushPendingCommands(); |
Tony Mantler | 5eb91a4 | 2016-09-28 13:55:15 -0700 | [diff] [blame] | 396 | } catch (Throwable tr) { |
| 397 | mCancelled.set(true); |
| 398 | throw tr; |
Tony Mantler | 78a8e9d | 2016-08-05 14:08:06 -0700 | [diff] [blame] | 399 | } finally { |
| 400 | postResult(result); |
| 401 | } |
| 402 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | } |
| 404 | }; |
| 405 | |
| 406 | mFuture = new FutureTask<Result>(mWorker) { |
| 407 | @Override |
| 408 | protected void done() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | try { |
Romain Guy | 657f513 | 2011-12-05 14:49:33 -0800 | [diff] [blame] | 410 | postResultIfNotInvoked(get()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | } catch (InterruptedException e) { |
| 412 | android.util.Log.w(LOG_TAG, e); |
| 413 | } catch (ExecutionException e) { |
Adrian Roos | 44bc07d | 2015-02-11 16:20:26 +0100 | [diff] [blame] | 414 | throw new RuntimeException("An error occurred while executing doInBackground()", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 415 | e.getCause()); |
| 416 | } catch (CancellationException e) { |
Romain Guy | 5ba812b | 2011-01-10 13:33:12 -0800 | [diff] [blame] | 417 | postResultIfNotInvoked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | } |
| 420 | }; |
| 421 | } |
| 422 | |
Romain Guy | 5ba812b | 2011-01-10 13:33:12 -0800 | [diff] [blame] | 423 | private void postResultIfNotInvoked(Result result) { |
| 424 | final boolean wasTaskInvoked = mTaskInvoked.get(); |
| 425 | if (!wasTaskInvoked) { |
| 426 | postResult(result); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | private Result postResult(Result result) { |
Romain Guy | 657f513 | 2011-12-05 14:49:33 -0800 | [diff] [blame] | 431 | @SuppressWarnings("unchecked") |
Jeff Brown | bba231d | 2014-11-14 15:49:45 -0800 | [diff] [blame] | 432 | Message message = getHandler().obtainMessage(MESSAGE_POST_RESULT, |
Romain Guy | 5ba812b | 2011-01-10 13:33:12 -0800 | [diff] [blame] | 433 | new AsyncTaskResult<Result>(this, result)); |
| 434 | message.sendToTarget(); |
| 435 | return result; |
| 436 | } |
| 437 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 438 | /** |
| 439 | * Returns the current status of this task. |
| 440 | * |
| 441 | * @return The current status. |
| 442 | */ |
| 443 | public final Status getStatus() { |
| 444 | return mStatus; |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * Override this method to perform a computation on a background thread. The |
| 449 | * specified parameters are the parameters passed to {@link #execute} |
| 450 | * by the caller of this task. |
| 451 | * |
Hans Boehm | 13060ff | 2019-01-31 17:22:55 -0800 | [diff] [blame] | 452 | * This will normally run on a background thread. But to better |
| 453 | * support testing frameworks, it is recommended that this also tolerates |
| 454 | * direct execution on the foreground thread, as part of the {@link #execute} call. |
| 455 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | * This method can call {@link #publishProgress} to publish updates |
Romain Guy | 0bbd8d8 | 2011-10-11 18:13:37 -0700 | [diff] [blame] | 457 | * on the UI thread. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | * |
| 459 | * @param params The parameters of the task. |
| 460 | * |
| 461 | * @return A result, defined by the subclass of this task. |
| 462 | * |
| 463 | * @see #onPreExecute() |
| 464 | * @see #onPostExecute |
| 465 | * @see #publishProgress |
| 466 | */ |
Tor Norbye | 83c6896 | 2015-03-10 20:55:31 -0700 | [diff] [blame] | 467 | @WorkerThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 468 | protected abstract Result doInBackground(Params... params); |
| 469 | |
| 470 | /** |
Romain Guy | 0bbd8d8 | 2011-10-11 18:13:37 -0700 | [diff] [blame] | 471 | * Runs on the UI thread before {@link #doInBackground}. |
Hans Boehm | 13060ff | 2019-01-31 17:22:55 -0800 | [diff] [blame] | 472 | * Invoked directly by {@link #execute} or {@link #executeOnExecutor}. |
| 473 | * The default version does nothing. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | * |
| 475 | * @see #onPostExecute |
| 476 | * @see #doInBackground |
| 477 | */ |
Tor Norbye | 83c6896 | 2015-03-10 20:55:31 -0700 | [diff] [blame] | 478 | @MainThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | protected void onPreExecute() { |
| 480 | } |
| 481 | |
| 482 | /** |
Romain Guy | 0bbd8d8 | 2011-10-11 18:13:37 -0700 | [diff] [blame] | 483 | * <p>Runs on the UI thread after {@link #doInBackground}. The |
Hans Boehm | 13060ff | 2019-01-31 17:22:55 -0800 | [diff] [blame] | 484 | * specified result is the value returned by {@link #doInBackground}. |
| 485 | * To better support testing frameworks, it is recommended that this be |
| 486 | * written to tolerate direct execution as part of the execute() call. |
| 487 | * The default version does nothing.</p> |
Romain Guy | e95003e | 2011-01-09 13:53:06 -0800 | [diff] [blame] | 488 | * |
| 489 | * <p>This method won't be invoked if the task was cancelled.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 490 | * |
| 491 | * @param result The result of the operation computed by {@link #doInBackground}. |
| 492 | * |
| 493 | * @see #onPreExecute |
| 494 | * @see #doInBackground |
Romain Guy | 5ba812b | 2011-01-10 13:33:12 -0800 | [diff] [blame] | 495 | * @see #onCancelled(Object) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 496 | */ |
| 497 | @SuppressWarnings({"UnusedDeclaration"}) |
Tor Norbye | 83c6896 | 2015-03-10 20:55:31 -0700 | [diff] [blame] | 498 | @MainThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | protected void onPostExecute(Result result) { |
| 500 | } |
| 501 | |
| 502 | /** |
Romain Guy | 0bbd8d8 | 2011-10-11 18:13:37 -0700 | [diff] [blame] | 503 | * Runs on the UI thread after {@link #publishProgress} is invoked. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | * The specified values are the values passed to {@link #publishProgress}. |
Hans Boehm | 13060ff | 2019-01-31 17:22:55 -0800 | [diff] [blame] | 505 | * The default version does nothing. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | * |
| 507 | * @param values The values indicating progress. |
| 508 | * |
| 509 | * @see #publishProgress |
| 510 | * @see #doInBackground |
| 511 | */ |
| 512 | @SuppressWarnings({"UnusedDeclaration"}) |
Tor Norbye | 83c6896 | 2015-03-10 20:55:31 -0700 | [diff] [blame] | 513 | @MainThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 514 | protected void onProgressUpdate(Progress... values) { |
| 515 | } |
| 516 | |
| 517 | /** |
Romain Guy | 0bbd8d8 | 2011-10-11 18:13:37 -0700 | [diff] [blame] | 518 | * <p>Runs on the UI thread after {@link #cancel(boolean)} is invoked and |
Romain Guy | 5ba812b | 2011-01-10 13:33:12 -0800 | [diff] [blame] | 519 | * {@link #doInBackground(Object[])} has finished.</p> |
| 520 | * |
| 521 | * <p>The default implementation simply invokes {@link #onCancelled()} and |
| 522 | * ignores the result. If you write your own implementation, do not call |
| 523 | * <code>super.onCancelled(result)</code>.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 524 | * |
Romain Guy | 5ba812b | 2011-01-10 13:33:12 -0800 | [diff] [blame] | 525 | * @param result The result, if any, computed in |
| 526 | * {@link #doInBackground(Object[])}, can be null |
| 527 | * |
| 528 | * @see #cancel(boolean) |
| 529 | * @see #isCancelled() |
| 530 | */ |
| 531 | @SuppressWarnings({"UnusedParameters"}) |
Tor Norbye | 83c6896 | 2015-03-10 20:55:31 -0700 | [diff] [blame] | 532 | @MainThread |
Romain Guy | 5ba812b | 2011-01-10 13:33:12 -0800 | [diff] [blame] | 533 | protected void onCancelled(Result result) { |
| 534 | onCancelled(); |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * <p>Applications should preferably override {@link #onCancelled(Object)}. |
| 539 | * This method is invoked by the default implementation of |
Hans Boehm | 13060ff | 2019-01-31 17:22:55 -0800 | [diff] [blame] | 540 | * {@link #onCancelled(Object)}. |
| 541 | * The default version does nothing.</p> |
Romain Guy | 5ba812b | 2011-01-10 13:33:12 -0800 | [diff] [blame] | 542 | * |
Romain Guy | 0bbd8d8 | 2011-10-11 18:13:37 -0700 | [diff] [blame] | 543 | * <p>Runs on the UI thread after {@link #cancel(boolean)} is invoked and |
Romain Guy | 5ba812b | 2011-01-10 13:33:12 -0800 | [diff] [blame] | 544 | * {@link #doInBackground(Object[])} has finished.</p> |
| 545 | * |
| 546 | * @see #onCancelled(Object) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | * @see #cancel(boolean) |
| 548 | * @see #isCancelled() |
| 549 | */ |
Tor Norbye | 83c6896 | 2015-03-10 20:55:31 -0700 | [diff] [blame] | 550 | @MainThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 551 | protected void onCancelled() { |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * Returns <tt>true</tt> if this task was cancelled before it completed |
Romain Guy | e95003e | 2011-01-09 13:53:06 -0800 | [diff] [blame] | 556 | * normally. If you are calling {@link #cancel(boolean)} on the task, |
| 557 | * the value returned by this method should be checked periodically from |
| 558 | * {@link #doInBackground(Object[])} to end the task as soon as possible. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 559 | * |
| 560 | * @return <tt>true</tt> if task was cancelled before it completed |
| 561 | * |
| 562 | * @see #cancel(boolean) |
| 563 | */ |
| 564 | public final boolean isCancelled() { |
Romain Guy | 657f513 | 2011-12-05 14:49:33 -0800 | [diff] [blame] | 565 | return mCancelled.get(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | /** |
Romain Guy | e95003e | 2011-01-09 13:53:06 -0800 | [diff] [blame] | 569 | * <p>Attempts to cancel execution of this task. This attempt will |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 570 | * fail if the task has already completed, already been cancelled, |
| 571 | * or could not be cancelled for some other reason. If successful, |
| 572 | * and this task has not started when <tt>cancel</tt> is called, |
Romain Guy | e95003e | 2011-01-09 13:53:06 -0800 | [diff] [blame] | 573 | * this task should never run. If the task has already started, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 574 | * then the <tt>mayInterruptIfRunning</tt> parameter determines |
| 575 | * whether the thread executing this task should be interrupted in |
Romain Guy | e95003e | 2011-01-09 13:53:06 -0800 | [diff] [blame] | 576 | * an attempt to stop the task.</p> |
| 577 | * |
Romain Guy | 5ba812b | 2011-01-10 13:33:12 -0800 | [diff] [blame] | 578 | * <p>Calling this method will result in {@link #onCancelled(Object)} being |
Hans Boehm | 13060ff | 2019-01-31 17:22:55 -0800 | [diff] [blame] | 579 | * invoked on the UI thread after {@link #doInBackground(Object[])} returns. |
| 580 | * Calling this method guarantees that onPostExecute(Object) is never |
| 581 | * subsequently invoked, even if <tt>cancel</tt> returns false, but |
| 582 | * {@link #onPostExecute} has not yet run. To finish the |
| 583 | * task as early as possible, check {@link #isCancelled()} periodically from |
| 584 | * {@link #doInBackground(Object[])}.</p> |
| 585 | * |
| 586 | * <p>This only requests cancellation. It never waits for a running |
| 587 | * background task to terminate, even if <tt>mayInterruptIfRunning</tt> is |
| 588 | * true.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 589 | * |
| 590 | * @param mayInterruptIfRunning <tt>true</tt> if the thread executing this |
| 591 | * task should be interrupted; otherwise, in-progress tasks are allowed |
| 592 | * to complete. |
| 593 | * |
| 594 | * @return <tt>false</tt> if the task could not be cancelled, |
| 595 | * typically because it has already completed normally; |
| 596 | * <tt>true</tt> otherwise |
| 597 | * |
| 598 | * @see #isCancelled() |
Romain Guy | 5ba812b | 2011-01-10 13:33:12 -0800 | [diff] [blame] | 599 | * @see #onCancelled(Object) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 600 | */ |
| 601 | public final boolean cancel(boolean mayInterruptIfRunning) { |
Romain Guy | 657f513 | 2011-12-05 14:49:33 -0800 | [diff] [blame] | 602 | mCancelled.set(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 603 | return mFuture.cancel(mayInterruptIfRunning); |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * Waits if necessary for the computation to complete, and then |
| 608 | * retrieves its result. |
| 609 | * |
| 610 | * @return The computed result. |
| 611 | * |
| 612 | * @throws CancellationException If the computation was cancelled. |
| 613 | * @throws ExecutionException If the computation threw an exception. |
| 614 | * @throws InterruptedException If the current thread was interrupted |
| 615 | * while waiting. |
| 616 | */ |
| 617 | public final Result get() throws InterruptedException, ExecutionException { |
| 618 | return mFuture.get(); |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * Waits if necessary for at most the given time for the computation |
| 623 | * to complete, and then retrieves its result. |
| 624 | * |
| 625 | * @param timeout Time to wait before cancelling the operation. |
| 626 | * @param unit The time unit for the timeout. |
| 627 | * |
| 628 | * @return The computed result. |
| 629 | * |
| 630 | * @throws CancellationException If the computation was cancelled. |
| 631 | * @throws ExecutionException If the computation threw an exception. |
| 632 | * @throws InterruptedException If the current thread was interrupted |
| 633 | * while waiting. |
| 634 | * @throws TimeoutException If the wait timed out. |
| 635 | */ |
| 636 | public final Result get(long timeout, TimeUnit unit) throws InterruptedException, |
| 637 | ExecutionException, TimeoutException { |
| 638 | return mFuture.get(timeout, unit); |
| 639 | } |
| 640 | |
| 641 | /** |
| 642 | * Executes the task with the specified parameters. The task returns |
Dianne Hackborn | 96438cd | 2011-01-25 21:42:37 -0800 | [diff] [blame] | 643 | * itself (this) so that the caller can keep a reference to it. |
| 644 | * |
| 645 | * <p>Note: this function schedules the task on a queue for a single background |
| 646 | * thread or pool of threads depending on the platform version. When first |
| 647 | * introduced, AsyncTasks were executed serially on a single background thread. |
| 648 | * Starting with {@link android.os.Build.VERSION_CODES#DONUT}, this was changed |
Romain Guy | 2c1b8c7 | 2012-05-21 10:43:26 -0700 | [diff] [blame] | 649 | * to a pool of threads allowing multiple tasks to operate in parallel. Starting |
| 650 | * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, tasks are back to being |
| 651 | * executed on a single thread to avoid common application errors caused |
Dianne Hackborn | 96438cd | 2011-01-25 21:42:37 -0800 | [diff] [blame] | 652 | * by parallel execution. If you truly want parallel execution, you can use |
| 653 | * the {@link #executeOnExecutor} version of this method |
Romain Guy | 2c1b8c7 | 2012-05-21 10:43:26 -0700 | [diff] [blame] | 654 | * with {@link #THREAD_POOL_EXECUTOR}; however, see commentary there for warnings |
| 655 | * on its use. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 656 | * |
Romain Guy | 0bbd8d8 | 2011-10-11 18:13:37 -0700 | [diff] [blame] | 657 | * <p>This method must be invoked on the UI thread. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 658 | * |
| 659 | * @param params The parameters of the task. |
| 660 | * |
| 661 | * @return This instance of AsyncTask. |
| 662 | * |
| 663 | * @throws IllegalStateException If {@link #getStatus()} returns either |
Romain Guy | 0bbd8d8 | 2011-10-11 18:13:37 -0700 | [diff] [blame] | 664 | * {@link AsyncTask.Status#RUNNING} or {@link AsyncTask.Status#FINISHED}. |
Romain Guy | 2c1b8c7 | 2012-05-21 10:43:26 -0700 | [diff] [blame] | 665 | * |
| 666 | * @see #executeOnExecutor(java.util.concurrent.Executor, Object[]) |
| 667 | * @see #execute(Runnable) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 668 | */ |
Tor Norbye | 83c6896 | 2015-03-10 20:55:31 -0700 | [diff] [blame] | 669 | @MainThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 670 | public final AsyncTask<Params, Progress, Result> execute(Params... params) { |
Joe Onorato | d630f10 | 2011-03-17 18:42:26 -0700 | [diff] [blame] | 671 | return executeOnExecutor(sDefaultExecutor, params); |
Joe Onorato | 81de61b | 2011-01-16 13:04:51 -0800 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | /** |
| 675 | * Executes the task with the specified parameters. The task returns |
| 676 | * itself (this) so that the caller can keep a reference to it. |
Dianne Hackborn | 96438cd | 2011-01-25 21:42:37 -0800 | [diff] [blame] | 677 | * |
| 678 | * <p>This method is typically used with {@link #THREAD_POOL_EXECUTOR} to |
| 679 | * allow multiple tasks to run in parallel on a pool of threads managed by |
| 680 | * AsyncTask, however you can also use your own {@link Executor} for custom |
| 681 | * behavior. |
| 682 | * |
| 683 | * <p><em>Warning:</em> Allowing multiple tasks to run in parallel from |
| 684 | * a thread pool is generally <em>not</em> what one wants, because the order |
| 685 | * of their operation is not defined. For example, if these tasks are used |
| 686 | * to modify any state in common (such as writing a file due to a button click), |
| 687 | * there are no guarantees on the order of the modifications. |
| 688 | * Without careful work it is possible in rare cases for the newer version |
| 689 | * of the data to be over-written by an older one, leading to obscure data |
| 690 | * loss and stability issues. Such changes are best |
| 691 | * executed in serial; to guarantee such work is serialized regardless of |
| 692 | * platform version you can use this function with {@link #SERIAL_EXECUTOR}. |
Joe Onorato | 81de61b | 2011-01-16 13:04:51 -0800 | [diff] [blame] | 693 | * |
Romain Guy | 0bbd8d8 | 2011-10-11 18:13:37 -0700 | [diff] [blame] | 694 | * <p>This method must be invoked on the UI thread. |
Joe Onorato | 81de61b | 2011-01-16 13:04:51 -0800 | [diff] [blame] | 695 | * |
| 696 | * @param exec The executor to use. {@link #THREAD_POOL_EXECUTOR} is available as a |
| 697 | * convenient process-wide thread pool for tasks that are loosely coupled. |
| 698 | * @param params The parameters of the task. |
| 699 | * |
| 700 | * @return This instance of AsyncTask. |
| 701 | * |
| 702 | * @throws IllegalStateException If {@link #getStatus()} returns either |
Romain Guy | 0bbd8d8 | 2011-10-11 18:13:37 -0700 | [diff] [blame] | 703 | * {@link AsyncTask.Status#RUNNING} or {@link AsyncTask.Status#FINISHED}. |
Romain Guy | 2c1b8c7 | 2012-05-21 10:43:26 -0700 | [diff] [blame] | 704 | * |
| 705 | * @see #execute(Object[]) |
Joe Onorato | 81de61b | 2011-01-16 13:04:51 -0800 | [diff] [blame] | 706 | */ |
Tor Norbye | 83c6896 | 2015-03-10 20:55:31 -0700 | [diff] [blame] | 707 | @MainThread |
Joe Onorato | 81de61b | 2011-01-16 13:04:51 -0800 | [diff] [blame] | 708 | public final AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec, |
| 709 | Params... params) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 710 | if (mStatus != Status.PENDING) { |
| 711 | switch (mStatus) { |
| 712 | case RUNNING: |
| 713 | throw new IllegalStateException("Cannot execute task:" |
| 714 | + " the task is already running."); |
| 715 | case FINISHED: |
| 716 | throw new IllegalStateException("Cannot execute task:" |
| 717 | + " the task has already been executed " |
| 718 | + "(a task can be executed only once)"); |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | mStatus = Status.RUNNING; |
| 723 | |
| 724 | onPreExecute(); |
| 725 | |
| 726 | mWorker.mParams = params; |
Joe Onorato | 81de61b | 2011-01-16 13:04:51 -0800 | [diff] [blame] | 727 | exec.execute(mFuture); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 728 | |
| 729 | return this; |
| 730 | } |
| 731 | |
| 732 | /** |
Dianne Hackborn | 96438cd | 2011-01-25 21:42:37 -0800 | [diff] [blame] | 733 | * Convenience version of {@link #execute(Object...)} for use with |
Romain Guy | 2c1b8c7 | 2012-05-21 10:43:26 -0700 | [diff] [blame] | 734 | * a simple Runnable object. See {@link #execute(Object[])} for more |
| 735 | * information on the order of execution. |
| 736 | * |
| 737 | * @see #execute(Object[]) |
| 738 | * @see #executeOnExecutor(java.util.concurrent.Executor, Object[]) |
Joe Onorato | 81de61b | 2011-01-16 13:04:51 -0800 | [diff] [blame] | 739 | */ |
Tor Norbye | 83c6896 | 2015-03-10 20:55:31 -0700 | [diff] [blame] | 740 | @MainThread |
Joe Onorato | 81de61b | 2011-01-16 13:04:51 -0800 | [diff] [blame] | 741 | public static void execute(Runnable runnable) { |
Joe Onorato | d630f10 | 2011-03-17 18:42:26 -0700 | [diff] [blame] | 742 | sDefaultExecutor.execute(runnable); |
Joe Onorato | 81de61b | 2011-01-16 13:04:51 -0800 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 746 | * This method can be invoked from {@link #doInBackground} to |
Romain Guy | 0bbd8d8 | 2011-10-11 18:13:37 -0700 | [diff] [blame] | 747 | * publish updates on the UI thread while the background computation is |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 748 | * still running. Each call to this method will trigger the execution of |
Romain Guy | 0bbd8d8 | 2011-10-11 18:13:37 -0700 | [diff] [blame] | 749 | * {@link #onProgressUpdate} on the UI thread. |
Dmitri Plotnikov | bef9c7a | 2010-06-16 15:38:07 -0700 | [diff] [blame] | 750 | * |
John Spurlock | 3390018 | 2014-01-02 11:04:18 -0500 | [diff] [blame] | 751 | * {@link #onProgressUpdate} will not be called if the task has been |
Romain Guy | 4aaf8ec | 2010-06-03 10:56:41 -0700 | [diff] [blame] | 752 | * canceled. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 753 | * |
| 754 | * @param values The progress values to update the UI with. |
| 755 | * |
| 756 | * @see #onProgressUpdate |
| 757 | * @see #doInBackground |
| 758 | */ |
Tor Norbye | 83c6896 | 2015-03-10 20:55:31 -0700 | [diff] [blame] | 759 | @WorkerThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 760 | protected final void publishProgress(Progress... values) { |
Romain Guy | 4aaf8ec | 2010-06-03 10:56:41 -0700 | [diff] [blame] | 761 | if (!isCancelled()) { |
Jeff Brown | bba231d | 2014-11-14 15:49:45 -0800 | [diff] [blame] | 762 | getHandler().obtainMessage(MESSAGE_POST_PROGRESS, |
Romain Guy | 4aaf8ec | 2010-06-03 10:56:41 -0700 | [diff] [blame] | 763 | new AsyncTaskResult<Progress>(this, values)).sendToTarget(); |
| 764 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | private void finish(Result result) { |
Romain Guy | e95003e | 2011-01-09 13:53:06 -0800 | [diff] [blame] | 768 | if (isCancelled()) { |
Romain Guy | 5ba812b | 2011-01-10 13:33:12 -0800 | [diff] [blame] | 769 | onCancelled(result); |
Romain Guy | e95003e | 2011-01-09 13:53:06 -0800 | [diff] [blame] | 770 | } else { |
| 771 | onPostExecute(result); |
| 772 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 773 | mStatus = Status.FINISHED; |
| 774 | } |
| 775 | |
| 776 | private static class InternalHandler extends Handler { |
Makoto Onuki | 1488a3a | 2017-05-24 12:25:46 -0700 | [diff] [blame] | 777 | public InternalHandler(Looper looper) { |
| 778 | super(looper); |
Jeff Brown | bba231d | 2014-11-14 15:49:45 -0800 | [diff] [blame] | 779 | } |
| 780 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 781 | @SuppressWarnings({"unchecked", "RawUseOfParameterizedType"}) |
| 782 | @Override |
| 783 | public void handleMessage(Message msg) { |
Jeff Brown | bba231d | 2014-11-14 15:49:45 -0800 | [diff] [blame] | 784 | AsyncTaskResult<?> result = (AsyncTaskResult<?>) msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 785 | switch (msg.what) { |
| 786 | case MESSAGE_POST_RESULT: |
| 787 | // There is only one result |
| 788 | result.mTask.finish(result.mData[0]); |
| 789 | break; |
| 790 | case MESSAGE_POST_PROGRESS: |
| 791 | result.mTask.onProgressUpdate(result.mData); |
| 792 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | private static abstract class WorkerRunnable<Params, Result> implements Callable<Result> { |
| 798 | Params[] mParams; |
| 799 | } |
| 800 | |
| 801 | @SuppressWarnings({"RawUseOfParameterizedType"}) |
| 802 | private static class AsyncTaskResult<Data> { |
| 803 | final AsyncTask mTask; |
| 804 | final Data[] mData; |
| 805 | |
| 806 | AsyncTaskResult(AsyncTask task, Data... data) { |
| 807 | mTask = task; |
| 808 | mData = data; |
| 809 | } |
| 810 | } |
| 811 | } |