diff options
| author | Romain Guy <romainguy@google.com> | 2012-05-21 10:43:26 -0700 |
|---|---|---|
| committer | Romain Guy <romainguy@google.com> | 2012-05-21 10:43:26 -0700 |
| commit | 2c1b8c744daf1dfb3cc545effaf9a43e342a3d38 (patch) | |
| tree | dfc05dd2495248fda92676eff9a1a19aafd14e8d /core/java/android/os/AsyncTask.java | |
| parent | 29bb6d925bcaa562212b3fe41de249c3dfb64354 (diff) | |
Update AsyncTask documentation
Change-Id: I678506309f027bb12d0c3f42436a60611aca5d8c
Diffstat (limited to 'core/java/android/os/AsyncTask.java')
| -rw-r--r-- | core/java/android/os/AsyncTask.java | 40 |
1 files changed, 34 insertions, 6 deletions
diff --git a/core/java/android/os/AsyncTask.java b/core/java/android/os/AsyncTask.java index 97ed235d5ff6..69e1de958fb5 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.</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> pacakge 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>, @@ -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; * <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> */ public abstract class AsyncTask<Params, Progress, Result> { private static final String LOG_TAG = "AsyncTask"; @@ -491,13 +510,13 @@ public abstract class AsyncTask<Params, Progress, Result> { * 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. * * <p>This method must be invoked on the UI thread. * @@ -507,6 +526,9 @@ public abstract class AsyncTask<Params, Progress, Result> { * * @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<Params, Progress, Result> execute(Params... params) { return executeOnExecutor(sDefaultExecutor, params); @@ -542,6 +564,8 @@ public abstract class AsyncTask<Params, Progress, Result> { * * @throws IllegalStateException If {@link #getStatus()} returns either * {@link AsyncTask.Status#RUNNING} or {@link AsyncTask.Status#FINISHED}. + * + * @see #execute(Object[]) */ public final AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec, Params... params) { @@ -569,7 +593,11 @@ public abstract class AsyncTask<Params, Progress, Result> { /** * 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); |
