From 13060ffbd83b6611f4e7b3ffc3c96d4ac8c17f00 Mon Sep 17 00:00:00 2001 From: Hans Boehm Date: Thu, 31 Jan 2019 17:22:55 -0800 Subject: [PATCH] Improve AsyncTask API documentation Bug: 34278255 Test: TreeHugger Change-Id: I0347072112254f98d257a72ef1fc7c21b086df39 --- core/java/android/os/AsyncTask.java | 55 +++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/core/java/android/os/AsyncTask.java b/core/java/android/os/AsyncTask.java index 141d33bc41459..1f3369376b10d 100644 --- a/core/java/android/os/AsyncTask.java +++ b/core/java/android/os/AsyncTask.java @@ -19,6 +19,7 @@ package android.os; import android.annotation.MainThread; import android.annotation.Nullable; import android.annotation.WorkerThread; + import java.util.ArrayDeque; import java.util.concurrent.BlockingQueue; import java.util.concurrent.Callable; @@ -70,7 +71,7 @@ import java.util.concurrent.atomic.AtomicInteger; * protected Long doInBackground(URL... urls) { * int count = urls.length; * long totalSize = 0; - * for (int i = 0; i < count; i++) { + * for (int i = 0; i < count; i++) { * totalSize += Downloader.downloadFile(urls[i]); * publishProgress((int) ((i / (float) count) * 100)); * // Escape early if cancel() is called @@ -158,13 +159,22 @@ import java.util.concurrent.atomic.AtomicInteger; * * *

Memory observability

- *

AsyncTask guarantees that all callback calls are synchronized in such a way that the following - * operations are safe without explicit synchronizations.

+ *

AsyncTask guarantees that all callback calls are synchronized to ensure the following + * without explicit synchronizations.

* * *

Order of execution

@@ -388,6 +398,10 @@ public abstract class AsyncTask { * specified parameters are the parameters passed to {@link #execute} * by the caller of this task. * + * This will normally run on a background thread. But to better + * support testing frameworks, it is recommended that this also tolerates + * direct execution on the foreground thread, as part of the {@link #execute} call. + * * This method can call {@link #publishProgress} to publish updates * on the UI thread. * @@ -404,6 +418,8 @@ public abstract class AsyncTask { /** * Runs on the UI thread before {@link #doInBackground}. + * Invoked directly by {@link #execute} or {@link #executeOnExecutor}. + * The default version does nothing. * * @see #onPostExecute * @see #doInBackground @@ -414,7 +430,10 @@ public abstract class AsyncTask { /** *

Runs on the UI thread after {@link #doInBackground}. The - * specified result is the value returned by {@link #doInBackground}.

+ * specified result is the value returned by {@link #doInBackground}. + * To better support testing frameworks, it is recommended that this be + * written to tolerate direct execution as part of the execute() call. + * The default version does nothing.

* *

This method won't be invoked if the task was cancelled.

* @@ -432,6 +451,7 @@ public abstract class AsyncTask { /** * Runs on the UI thread after {@link #publishProgress} is invoked. * The specified values are the values passed to {@link #publishProgress}. + * The default version does nothing. * * @param values The values indicating progress. * @@ -466,7 +486,8 @@ public abstract class AsyncTask { /** *

Applications should preferably override {@link #onCancelled(Object)}. * This method is invoked by the default implementation of - * {@link #onCancelled(Object)}.

+ * {@link #onCancelled(Object)}. + * The default version does nothing.

* *

Runs on the UI thread after {@link #cancel(boolean)} is invoked and * {@link #doInBackground(Object[])} has finished.

@@ -504,12 +525,16 @@ public abstract class AsyncTask { * an attempt to stop the task.

* *

Calling this method will result in {@link #onCancelled(Object)} being - * invoked on the UI thread after {@link #doInBackground(Object[])} - * returns. Calling this method guarantees that {@link #onPostExecute(Object)} - * is never invoked. After invoking this method, you should check the - * value returned by {@link #isCancelled()} periodically from - * {@link #doInBackground(Object[])} to finish the task as early as - * possible.

+ * invoked on the UI thread after {@link #doInBackground(Object[])} returns. + * Calling this method guarantees that onPostExecute(Object) is never + * subsequently invoked, even if cancel returns false, but + * {@link #onPostExecute} has not yet run. To finish the + * task as early as possible, check {@link #isCancelled()} periodically from + * {@link #doInBackground(Object[])}.

+ * + *

This only requests cancellation. It never waits for a running + * background task to terminate, even if mayInterruptIfRunning is + * true.

* * @param mayInterruptIfRunning true if the thread executing this * task should be interrupted; otherwise, in-progress tasks are allowed