am d7e4ae48: Merge "Update AsyncTask documentation" into jb-dev

* commit 'd7e4ae48c576d5aaef75754b29a8b87c1f0d6427':
  Update AsyncTask documentation
This commit is contained in:
Romain Guy
2012-05-21 10:49:24 -07:00
committed by Android Git Automerger

View File

@@ -36,6 +36,13 @@ import java.util.concurrent.atomic.AtomicInteger;
* perform background operations and publish results on the UI thread without * perform background operations and publish results on the UI thread without
* having to manipulate threads and/or handlers.</p> * 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 * <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 * 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>, * 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++) { * for (int i = 0; i < count; i++) {
* totalSize += Downloader.downloadFile(urls[i]); * totalSize += Downloader.downloadFile(urls[i]);
* publishProgress((int) ((i / (float) count) * 100)); * publishProgress((int) ((i / (float) count) * 100));
* // Escape early if cancel() is called
* if (isCancelled()) break;
* } * }
* return totalSize; * return totalSize;
* } * }
@@ -154,6 +163,16 @@ import java.util.concurrent.atomic.AtomicInteger;
* <li>Set member fields in {@link #doInBackground}, and refer to them in * <li>Set member fields in {@link #doInBackground}, and refer to them in
* {@link #onProgressUpdate} and {@link #onPostExecute}. * {@link #onProgressUpdate} and {@link #onPostExecute}.
* </ul> * </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> { public abstract class AsyncTask<Params, Progress, Result> {
private static final String LOG_TAG = "AsyncTask"; 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 * thread or pool of threads depending on the platform version. When first
* introduced, AsyncTasks were executed serially on a single background thread. * introduced, AsyncTasks were executed serially on a single background thread.
* Starting with {@link android.os.Build.VERSION_CODES#DONUT}, this was changed * 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 * to a pool of threads allowing multiple tasks to operate in parallel. Starting
* {@link android.os.Build.VERSION_CODES#HONEYCOMB}, it is planned to change this * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, tasks are back to being
* back to a single thread to avoid common application errors caused * executed on a single thread to avoid common application errors caused
* by parallel execution. If you truly want parallel execution, you can use * by parallel execution. If you truly want parallel execution, you can use
* the {@link #executeOnExecutor} version of this method * the {@link #executeOnExecutor} version of this method
* with {@link #THREAD_POOL_EXECUTOR}; however, see commentary there for warnings on * with {@link #THREAD_POOL_EXECUTOR}; however, see commentary there for warnings
* its use. * on its use.
* *
* <p>This method must be invoked on the UI thread. * <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 * @throws IllegalStateException If {@link #getStatus()} returns either
* {@link AsyncTask.Status#RUNNING} or {@link AsyncTask.Status#FINISHED}. * {@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) { public final AsyncTask<Params, Progress, Result> execute(Params... params) {
return executeOnExecutor(sDefaultExecutor, params); return executeOnExecutor(sDefaultExecutor, params);
@@ -542,6 +564,8 @@ public abstract class AsyncTask<Params, Progress, Result> {
* *
* @throws IllegalStateException If {@link #getStatus()} returns either * @throws IllegalStateException If {@link #getStatus()} returns either
* {@link AsyncTask.Status#RUNNING} or {@link AsyncTask.Status#FINISHED}. * {@link AsyncTask.Status#RUNNING} or {@link AsyncTask.Status#FINISHED}.
*
* @see #execute(Object[])
*/ */
public final AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec, public final AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec,
Params... params) { Params... params) {
@@ -569,7 +593,11 @@ public abstract class AsyncTask<Params, Progress, Result> {
/** /**
* Convenience version of {@link #execute(Object...)} for use with * 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) { public static void execute(Runnable runnable) {
sDefaultExecutor.execute(runnable); sDefaultExecutor.execute(runnable);