Commit da8490d6 authored by Matthew Jones's avatar Matthew Jones Committed by Commit Bot

Revert "Android: removing default execute() on AsyncTask"

This reverts commit be62a5ba.

Reason for revert: Causes compile failure on many bots:

 ../../components/cronet/android/test/javaperftests/src/org/chromium/net/CronetPerfTestActivity.java:625: error: cannot find symbol
        new BenchmarkTask().execute();

https://ci.chromium.org/buildbot/chromium.android/Android%20Cronet%20Marshmallow%2064bit%20Perf/24049

Original change's description:
> 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/1172446
> Reviewed-by: agrieve <agrieve@chromium.org>
> Commit-Queue: Sam Maier <smaier@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#584189}

TBR=agrieve@chromium.org,smaier@chromium.org

Change-Id: I9184afddb3028649c88f9fdf80ea46c4f7cc7806
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: 869907
Reviewed-on: https://chromium-review.googlesource.com/1179352Reviewed-by: default avatarMatthew Jones <mdjones@chromium.org>
Commit-Queue: Matthew Jones <mdjones@chromium.org>
Cr-Commit-Position: refs/heads/master@{#584211}
parent 3e362e40
......@@ -29,13 +29,13 @@ public class BackgroundShadowAsyncTask<Result> extends ShadowAsyncTask<Result> {
@Override
@Implementation
public final AsyncTask<Result> executeOnExecutor(Executor e) {
public final AsyncTask<Result> execute() {
try {
return sExecutorService
.submit(new Callable<AsyncTask<Result>>() {
@Override
public AsyncTask<Result> call() throws Exception {
return BackgroundShadowAsyncTask.super.executeInRobolectric();
return BackgroundShadowAsyncTask.super.execute();
}
})
.get();
......@@ -45,6 +45,12 @@ public class BackgroundShadowAsyncTask<Result> extends ShadowAsyncTask<Result> {
}
}
@Override
@Implementation
public final AsyncTask<Result> executeOnExecutor(Executor e) {
return execute();
}
@Override
@Implementation
public final Result get() {
......
......@@ -22,6 +22,6 @@ public class CustomShadowAsyncTask<Result> extends ShadowAsyncTask<Result> {
@Override
@Implementation
public final AsyncTask<Result> executeOnExecutor(Executor executor) {
return super.executeInRobolectric();
return super.execute();
}
}
......@@ -143,7 +143,7 @@ import java.util.concurrent.atomic.AtomicInteger;
* <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>
* <li>The task instance must be created on the UI thread.</li>
* <li>{@link #executeOnExecutor(Executor)} must be invoked on the UI thread.</li>
* <li>{@link #execute} must be invoked on the UI thread.</li>
* <li>Do not call {@link #onPreExecute()}, {@link #onPostExecute},
* {@link #doInBackground} manually.</li>
* <li>The task can be executed only once (an exception will be thrown if
......@@ -207,6 +207,8 @@ public abstract class AsyncTask<Result> {
private static final int MESSAGE_POST_RESULT = 0x1;
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 final Callable<Result> mWorker;
......@@ -407,7 +409,9 @@ public abstract class AsyncTask<Result> {
}
/**
* Override this method to perform a computation on a background thread.
* Override this method to perform a computation on a background thread. The
* 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.
*
......@@ -560,6 +564,37 @@ public abstract class AsyncTask<Result> {
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
* itself (this) so that the caller can keep a reference to it.
......@@ -589,6 +624,8 @@ public abstract class AsyncTask<Result> {
*
* @throws IllegalStateException If {@link #getStatus()} returns either
* {@link AsyncTask.Status#RUNNING} or {@link AsyncTask.Status#FINISHED}.
*
* @see #execute()
*/
@SuppressWarnings({"MissingCasesInEnumSwitch"})
@MainThread
......@@ -614,6 +651,19 @@ public abstract class AsyncTask<Result> {
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) {
if (isCancelled()) {
onCancelled(result);
......
......@@ -88,7 +88,9 @@ public class ShadowAsyncTask<Result> {
return future.get(timeout, unit);
}
public AsyncTask<Result> executeInRobolectric() {
@SuppressWarnings("unchecked")
@Implementation
public AsyncTask<Result> execute() {
status = AsyncTask.Status.RUNNING;
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