Skip to content

Commit 2c1b8c7

Browse files
author
Romain Guy
committed
Update AsyncTask documentation
Change-Id: I678506309f027bb12d0c3f42436a60611aca5d8c
1 parent 29bb6d9 commit 2c1b8c7

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

core/java/android/os/AsyncTask.java

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@
3636
* perform background operations and publish results on the UI thread without
3737
* having to manipulate threads and/or handlers.</p>
3838
*
39+
* <p>AsyncTask is designed to be a helper class around {@link Thread} and {@link Handler}
40+
* and does not constitute a generic threading framework. AsyncTasks should ideally be
41+
* used for short operations (a few seconds at the most.) If you need to keep threads
42+
* running for long periods of time, it is highly recommended you use the various APIs
43+
* provided by the <code>java.util.concurrent</code> pacakge such as {@link Executor},
44+
* {@link ThreadPoolExecutor} and {@link FutureTask}.</p>
45+
*
3946
* <p>An asynchronous task is defined by a computation that runs on a background thread and
4047
* whose result is published on the UI thread. An asynchronous task is defined by 3 generic
4148
* types, called <code>Params</code>, <code>Progress</code> and <code>Result</code>,
@@ -63,6 +70,8 @@
6370
* for (int i = 0; i < count; i++) {
6471
* totalSize += Downloader.downloadFile(urls[i]);
6572
* publishProgress((int) ((i / (float) count) * 100));
73+
* // Escape early if cancel() is called
74+
* if (isCancelled()) break;
6675
* }
6776
* return totalSize;
6877
* }
@@ -154,6 +163,16 @@
154163
* <li>Set member fields in {@link #doInBackground}, and refer to them in
155164
* {@link #onProgressUpdate} and {@link #onPostExecute}.
156165
* </ul>
166+
*
167+
* <h2>Order of execution</h2>
168+
* <p>When first introduced, AsyncTasks were executed serially on a single background
169+
* thread. Starting with {@link android.os.Build.VERSION_CODES#DONUT}, this was changed
170+
* to a pool of threads allowing multiple tasks to operate in parallel. Starting with
171+
* {@link android.os.Build.VERSION_CODES#HONEYCOMB}, tasks are executed on a single
172+
* thread to avoid common application errors caused by parallel execution.</p>
173+
* <p>If you truly want parallel execution, you can invoke
174+
* {@link #executeOnExecutor(java.util.concurrent.Executor, Object[])} with
175+
* {@link #THREAD_POOL_EXECUTOR}.</p>
157176
*/
158177
public abstract class AsyncTask<Params, Progress, Result> {
159178
private static final String LOG_TAG = "AsyncTask";
@@ -491,13 +510,13 @@ public final Result get(long timeout, TimeUnit unit) throws InterruptedException
491510
* thread or pool of threads depending on the platform version. When first
492511
* introduced, AsyncTasks were executed serially on a single background thread.
493512
* Starting with {@link android.os.Build.VERSION_CODES#DONUT}, this was changed
494-
* to a pool of threads allowing multiple tasks to operate in parallel. After
495-
* {@link android.os.Build.VERSION_CODES#HONEYCOMB}, it is planned to change this
496-
* back to a single thread to avoid common application errors caused
513+
* to a pool of threads allowing multiple tasks to operate in parallel. Starting
514+
* {@link android.os.Build.VERSION_CODES#HONEYCOMB}, tasks are back to being
515+
* executed on a single thread to avoid common application errors caused
497516
* by parallel execution. If you truly want parallel execution, you can use
498517
* the {@link #executeOnExecutor} version of this method
499-
* with {@link #THREAD_POOL_EXECUTOR}; however, see commentary there for warnings on
500-
* its use.
518+
* with {@link #THREAD_POOL_EXECUTOR}; however, see commentary there for warnings
519+
* on its use.
501520
*
502521
* <p>This method must be invoked on the UI thread.
503522
*
@@ -507,6 +526,9 @@ public final Result get(long timeout, TimeUnit unit) throws InterruptedException
507526
*
508527
* @throws IllegalStateException If {@link #getStatus()} returns either
509528
* {@link AsyncTask.Status#RUNNING} or {@link AsyncTask.Status#FINISHED}.
529+
*
530+
* @see #executeOnExecutor(java.util.concurrent.Executor, Object[])
531+
* @see #execute(Runnable)
510532
*/
511533
public final AsyncTask<Params, Progress, Result> execute(Params... params) {
512534
return executeOnExecutor(sDefaultExecutor, params);
@@ -542,6 +564,8 @@ public final AsyncTask<Params, Progress, Result> execute(Params... params) {
542564
*
543565
* @throws IllegalStateException If {@link #getStatus()} returns either
544566
* {@link AsyncTask.Status#RUNNING} or {@link AsyncTask.Status#FINISHED}.
567+
*
568+
* @see #execute(Object[])
545569
*/
546570
public final AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec,
547571
Params... params) {
@@ -569,7 +593,11 @@ public final AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec
569593

570594
/**
571595
* Convenience version of {@link #execute(Object...)} for use with
572-
* a simple Runnable object.
596+
* a simple Runnable object. See {@link #execute(Object[])} for more
597+
* information on the order of execution.
598+
*
599+
* @see #execute(Object[])
600+
* @see #executeOnExecutor(java.util.concurrent.Executor, Object[])
573601
*/
574602
public static void execute(Runnable runnable) {
575603
sDefaultExecutor.execute(runnable);

0 commit comments

Comments
 (0)