Commit be62a5ba authored by Sam Maier's avatar Sam Maier Committed by Commit Bot

Android: removing default execute() on AsyncTask

execute() is a confusing function, with a hidden implicit meaning that
changed in API 11. It would be better if everyone had to use the more
explicit executeOnExecutor() so their preference (serial or thread pool)
is called out.

Bug: 869907
Change-Id: I79be6d2b3114cb0520df1104ec5849f7c070c99e
Reviewed-on: https://chromium-review.googlesource.com/1172446Reviewed-by: default avataragrieve <agrieve@chromium.org>
Commit-Queue: Sam Maier <smaier@chromium.org>
Cr-Commit-Position: refs/heads/master@{#584189}
parent cd3122a6
...@@ -29,13 +29,13 @@ public class BackgroundShadowAsyncTask<Result> extends ShadowAsyncTask<Result> { ...@@ -29,13 +29,13 @@ public class BackgroundShadowAsyncTask<Result> extends ShadowAsyncTask<Result> {
@Override @Override
@Implementation @Implementation
public final AsyncTask<Result> execute() { public final AsyncTask<Result> executeOnExecutor(Executor e) {
try { try {
return sExecutorService return sExecutorService
.submit(new Callable<AsyncTask<Result>>() { .submit(new Callable<AsyncTask<Result>>() {
@Override @Override
public AsyncTask<Result> call() throws Exception { public AsyncTask<Result> call() throws Exception {
return BackgroundShadowAsyncTask.super.execute(); return BackgroundShadowAsyncTask.super.executeInRobolectric();
} }
}) })
.get(); .get();
...@@ -45,12 +45,6 @@ public class BackgroundShadowAsyncTask<Result> extends ShadowAsyncTask<Result> { ...@@ -45,12 +45,6 @@ public class BackgroundShadowAsyncTask<Result> extends ShadowAsyncTask<Result> {
} }
} }
@Override
@Implementation
public final AsyncTask<Result> executeOnExecutor(Executor e) {
return execute();
}
@Override @Override
@Implementation @Implementation
public final Result get() { public final Result get() {
......
...@@ -22,6 +22,6 @@ public class CustomShadowAsyncTask<Result> extends ShadowAsyncTask<Result> { ...@@ -22,6 +22,6 @@ public class CustomShadowAsyncTask<Result> extends ShadowAsyncTask<Result> {
@Override @Override
@Implementation @Implementation
public final AsyncTask<Result> executeOnExecutor(Executor executor) { public final AsyncTask<Result> executeOnExecutor(Executor executor) {
return super.execute(); return super.executeInRobolectric();
} }
} }
...@@ -143,7 +143,7 @@ import java.util.concurrent.atomic.AtomicInteger; ...@@ -143,7 +143,7 @@ import java.util.concurrent.atomic.AtomicInteger;
* <li>The AsyncTask class must be loaded on the UI thread. This is done * <li>The AsyncTask class must be loaded on the UI thread. This is done
* automatically as of {@link android.os.Build.VERSION_CODES#JELLY_BEAN}.</li> * automatically as of {@link android.os.Build.VERSION_CODES#JELLY_BEAN}.</li>
* <li>The task instance must be created on the UI thread.</li> * <li>The task instance must be created on the UI thread.</li>
* <li>{@link #execute} must be invoked on the UI thread.</li> * <li>{@link #executeOnExecutor(Executor)} must be invoked on the UI thread.</li>
* <li>Do not call {@link #onPreExecute()}, {@link #onPostExecute}, * <li>Do not call {@link #onPreExecute()}, {@link #onPostExecute},
* {@link #doInBackground} manually.</li> * {@link #doInBackground} manually.</li>
* <li>The task can be executed only once (an exception will be thrown if * <li>The task can be executed only once (an exception will be thrown if
...@@ -207,8 +207,6 @@ public abstract class AsyncTask<Result> { ...@@ -207,8 +207,6 @@ public abstract class AsyncTask<Result> {
private static final int MESSAGE_POST_RESULT = 0x1; private static final int MESSAGE_POST_RESULT = 0x1;
private static final int MESSAGE_POST_PROGRESS = 0x2; private static final int MESSAGE_POST_PROGRESS = 0x2;
private static volatile Executor sDefaultExecutor = SERIAL_EXECUTOR;
private static final StealRunnableHandler STEAL_RUNNABLE_HANDLER = new StealRunnableHandler(); private static final StealRunnableHandler STEAL_RUNNABLE_HANDLER = new StealRunnableHandler();
private final Callable<Result> mWorker; private final Callable<Result> mWorker;
...@@ -409,9 +407,7 @@ public abstract class AsyncTask<Result> { ...@@ -409,9 +407,7 @@ public abstract class AsyncTask<Result> {
} }
/** /**
* Override this method to perform a computation on a background thread. The * Override this method to perform a computation on a background thread.
* specified parameters are the parameters passed to {@link #execute}
* by the caller of this task.
* *
* @return A result, defined by the subclass of this task. * @return A result, defined by the subclass of this task.
* *
...@@ -564,37 +560,6 @@ public abstract class AsyncTask<Result> { ...@@ -564,37 +560,6 @@ public abstract class AsyncTask<Result> {
return mFuture.get(timeout, unit); return mFuture.get(timeout, unit);
} }
/**
* Executes the task with the specified parameters. The task returns
* itself (this) so that the caller can keep a reference to it.
*
* <p>Note: this function schedules the task on a queue for a single background
* 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. 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.
*
* <p>This method must be invoked on the UI thread.
*
* @return This instance of AsyncTask.
*
* @throws IllegalStateException If {@link #getStatus()} returns either
* {@link AsyncTask.Status#RUNNING} or {@link AsyncTask.Status#FINISHED}.
*
* @see #executeOnExecutor(java.util.concurrent.Executor)
* @see #execute(Runnable)
*/
@MainThread
public final AsyncTask<Result> execute() {
return executeOnExecutor(sDefaultExecutor);
}
/** /**
* Executes the task with the specified parameters. The task returns * Executes the task with the specified parameters. The task returns
* itself (this) so that the caller can keep a reference to it. * itself (this) so that the caller can keep a reference to it.
...@@ -624,8 +589,6 @@ public abstract class AsyncTask<Result> { ...@@ -624,8 +589,6 @@ public abstract class AsyncTask<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()
*/ */
@SuppressWarnings({"MissingCasesInEnumSwitch"}) @SuppressWarnings({"MissingCasesInEnumSwitch"})
@MainThread @MainThread
...@@ -651,19 +614,6 @@ public abstract class AsyncTask<Result> { ...@@ -651,19 +614,6 @@ public abstract class AsyncTask<Result> {
return this; return this;
} }
/**
* Convenience version of {@link #execute()} for use with
* a simple Runnable object. See {@link #execute()} for more
* information on the order of execution.
*
* @see #execute()
* @see #executeOnExecutor(java.util.concurrent.Executor)
*/
@MainThread
public static void execute(Runnable runnable) {
sDefaultExecutor.execute(runnable);
}
private void finish(Result result) { private void finish(Result result) {
if (isCancelled()) { if (isCancelled()) {
onCancelled(result); onCancelled(result);
......
...@@ -88,9 +88,7 @@ public class ShadowAsyncTask<Result> { ...@@ -88,9 +88,7 @@ public class ShadowAsyncTask<Result> {
return future.get(timeout, unit); return future.get(timeout, unit);
} }
@SuppressWarnings("unchecked") public AsyncTask<Result> executeInRobolectric() {
@Implementation
public AsyncTask<Result> execute() {
status = AsyncTask.Status.RUNNING; status = AsyncTask.Status.RUNNING;
getBridge().onPreExecute(); getBridge().onPreExecute();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment