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;
*
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 AsyncTaskThis method must be invoked on the UI thread.
*
@@ -507,6 +526,9 @@ public abstract class AsyncTask