From 2c1b8c744daf1dfb3cc545effaf9a43e342a3d38 Mon Sep 17 00:00:00 2001
From: Romain Guy
Date: Mon, 21 May 2012 10:43:26 -0700
Subject: [PATCH] Update AsyncTask documentation
Change-Id: I678506309f027bb12d0c3f42436a60611aca5d8c
---
core/java/android/os/AsyncTask.java | 40 ++++++++++++++++++++++++-----
1 file changed, 34 insertions(+), 6 deletions(-)
diff --git a/core/java/android/os/AsyncTask.java b/core/java/android/os/AsyncTask.java
index 97ed235d5ff62..69e1de958fb5c 100644
--- a/core/java/android/os/AsyncTask.java
+++ b/core/java/android/os/AsyncTask.java
@@ -36,6 +36,13 @@ import java.util.concurrent.atomic.AtomicInteger;
* perform background operations and publish results on the UI thread without
* having to manipulate threads and/or handlers.
*
+ * 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 java.util.concurrent pacakge such as {@link Executor},
+ * {@link ThreadPoolExecutor} and {@link FutureTask}.
+ *
* 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 Params, Progress and Result,
@@ -63,6 +70,8 @@ import java.util.concurrent.atomic.AtomicInteger;
* 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;
* }
@@ -154,6 +163,16 @@ import java.util.concurrent.atomic.AtomicInteger;
*
Set member fields in {@link #doInBackground}, and refer to them in
* {@link #onProgressUpdate} and {@link #onPostExecute}.
*
+ *
+ * Order of execution
+ * 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.
+ * If you truly want parallel execution, you can invoke
+ * {@link #executeOnExecutor(java.util.concurrent.Executor, Object[])} with
+ * {@link #THREAD_POOL_EXECUTOR}.
*/
public abstract class AsyncTask {
private static final String LOG_TAG = "AsyncTask";
@@ -491,13 +510,13 @@ public abstract class AsyncTask {
* thread or pool of threads depending on the platform version. 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. After
- * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, it is planned to change this
- * back to a single thread to avoid common application errors caused
+ * to a pool of threads allowing multiple tasks to operate in parallel. Starting
+ * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, tasks are back to being
+ * executed on a single thread to avoid common application errors caused
* by parallel execution. If you truly want parallel execution, you can use
* the {@link #executeOnExecutor} version of this method
- * with {@link #THREAD_POOL_EXECUTOR}; however, see commentary there for warnings on
- * its use.
+ * with {@link #THREAD_POOL_EXECUTOR}; however, see commentary there for warnings
+ * on its use.
*
* This method must be invoked on the UI thread.
*
@@ -507,6 +526,9 @@ public abstract class AsyncTask {
*
* @throws IllegalStateException If {@link #getStatus()} returns either
* {@link AsyncTask.Status#RUNNING} or {@link AsyncTask.Status#FINISHED}.
+ *
+ * @see #executeOnExecutor(java.util.concurrent.Executor, Object[])
+ * @see #execute(Runnable)
*/
public final AsyncTask execute(Params... params) {
return executeOnExecutor(sDefaultExecutor, params);
@@ -542,6 +564,8 @@ public abstract class AsyncTask {
*
* @throws IllegalStateException If {@link #getStatus()} returns either
* {@link AsyncTask.Status#RUNNING} or {@link AsyncTask.Status#FINISHED}.
+ *
+ * @see #execute(Object[])
*/
public final AsyncTask executeOnExecutor(Executor exec,
Params... params) {
@@ -569,7 +593,11 @@ public abstract class AsyncTask {
/**
* Convenience version of {@link #execute(Object...)} for use with
- * a simple Runnable object.
+ * a simple Runnable object. See {@link #execute(Object[])} for more
+ * information on the order of execution.
+ *
+ * @see #execute(Object[])
+ * @see #executeOnExecutor(java.util.concurrent.Executor, Object[])
*/
public static void execute(Runnable runnable) {
sDefaultExecutor.execute(runnable);