public abstract class OptionalExecutorTask<Params,Progress,Result>
extends java.lang.Object
| 限定符和类型 | 类和说明 |
|---|---|
static class |
OptionalExecutorTask.Status
Indicates the current status of the task.
|
| 限定符和类型 | 字段和说明 |
|---|---|
static java.util.concurrent.Executor |
THREAD_POOL_EXECUTOR
An
Executor that can be used to execute tasks in parallel. |
| 构造器和说明 |
|---|
OptionalExecutorTask()
Creates a new asynchronous task.
|
| 限定符和类型 | 方法和说明 |
|---|---|
boolean |
cancel(boolean mayInterruptIfRunning)
Attempts to cancel execution of this task.
|
protected abstract Result |
doInBackground(Params... params)
Override this method to perform a computation on a background thread.
|
OptionalExecutorTask<Params,Progress,Result> |
execute(Params... params)
Executes the task with the specified parameters.
|
static void |
execute(java.lang.Runnable runnable)
Convenience version of
execute(Object...) for use with
a simple Runnable object. |
OptionalExecutorTask<Params,Progress,Result> |
executeOnExecutor(java.util.concurrent.Executor exec,
Params... params)
Executes the task with the specified parameters.
|
Result |
get()
Waits if necessary for the computation to complete, and then
retrieves its result.
|
Result |
get(long timeout,
java.util.concurrent.TimeUnit unit)
Waits if necessary for at most the given time for the computation
to complete, and then retrieves its result.
|
OptionalExecutorTask.Status |
getStatus()
Returns the current status of this task.
|
static void |
init() |
boolean |
isCancelled()
Returns true if this task was cancelled before it completed
normally.
|
protected void |
onCancelled()
Applications should preferably override
onCancelled(Object). |
protected void |
onCancelled(Result result)
Runs on the UI thread after
cancel(boolean) is invoked and
doInBackground(Object[]) has finished. |
protected void |
onPostExecute(Result result)
Runs on the UI thread after
doInBackground(Params...). |
protected void |
onPreExecute()
Runs on the UI thread before
doInBackground(Params...). |
protected void |
onProgressUpdate(Progress... values)
Runs on the UI thread after
publishProgress(Progress...) is invoked. |
protected void |
publishProgress(Progress... values)
This method can be invoked from
doInBackground(Params...) to
publish updates on the UI thread while the background computation is
still running. |
static void |
setDefaultExecutor(java.util.concurrent.Executor exec) |
public static final java.util.concurrent.Executor THREAD_POOL_EXECUTOR
Executor that can be used to execute tasks in parallel.public OptionalExecutorTask()
public static void init()
public static void setDefaultExecutor(java.util.concurrent.Executor exec)
public final OptionalExecutorTask.Status getStatus()
protected abstract Result doInBackground(Params... params)
execute(Params...)
by the caller of this task.
This method can call publishProgress(Progress...) to publish updates
on the UI thread.params - The parameters of the task.onPreExecute(),
onPostExecute(Result),
publishProgress(Progress...)protected void onPreExecute()
doInBackground(Params...).protected void onPostExecute(Result result)
Runs on the UI thread after doInBackground(Params...). The
specified result is the value returned by doInBackground(Params...).
This method won't be invoked if the task was cancelled.
result - The result of the operation computed by doInBackground(Params...).onPreExecute(),
doInBackground(Params...),
onCancelled(Object)protected void onProgressUpdate(Progress... values)
publishProgress(Progress...) is invoked.
The specified values are the values passed to publishProgress(Progress...).values - The values indicating progress.publishProgress(Progress...),
doInBackground(Params...)protected void onCancelled(Result result)
Runs on the UI thread after cancel(boolean) is invoked and
doInBackground(Object[]) has finished.
The default implementation simply invokes onCancelled() and
ignores the result. If you write your own implementation, do not call
super.onCancelled(result).
result - The result, if any, computed in
doInBackground(Object[]), can be nullcancel(boolean),
isCancelled()protected void onCancelled()
Applications should preferably override onCancelled(Object).
This method is invoked by the default implementation of
onCancelled(Object).
Runs on the UI thread after cancel(boolean) is invoked and
doInBackground(Object[]) has finished.
public final boolean isCancelled()
cancel(boolean) on the task,
the value returned by this method should be checked periodically from
doInBackground(Object[]) to end the task as soon as possible.cancel(boolean)public final boolean cancel(boolean mayInterruptIfRunning)
Attempts to cancel execution of this task. This attempt will fail if the task has already completed, already been cancelled, or could not be cancelled for some other reason. If successful, and this task has not started when cancel is called, this task should never run. If the task has already started, then the mayInterruptIfRunning parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.
Calling this method will result in onCancelled(Object) being
invoked on the UI thread after doInBackground(Object[])
returns. Calling this method guarantees that onPostExecute(Object)
is never invoked. After invoking this method, you should check the
value returned by isCancelled() periodically from
doInBackground(Object[]) to finish the task as early as
possible.
mayInterruptIfRunning - true if the thread executing this
task should be interrupted; otherwise, in-progress tasks are allowed
to complete.isCancelled(),
onCancelled(Object)public final Result get() throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
java.util.concurrent.CancellationException - If the computation was cancelled.java.util.concurrent.ExecutionException - If the computation threw an exception.java.lang.InterruptedException - If the current thread was interrupted
while waiting.public final Result get(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutException
timeout - Time to wait before cancelling the operation.unit - The time unit for the timeout.java.util.concurrent.CancellationException - If the computation was cancelled.java.util.concurrent.ExecutionException - If the computation threw an exception.java.lang.InterruptedException - If the current thread was interrupted
while waiting.TimeoutException - If the wait timed out.java.util.concurrent.TimeoutExceptionpublic final OptionalExecutorTask<Params,Progress,Result> execute(Params... params)
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 Build.VERSION_CODES.DONUT, this was changed
to a pool of threads allowing multiple tasks to operate in parallel. After
Build.VERSION_CODES.HONEYCOMB, it is planned to change this
back to a single thread to avoid common application errors caused
by parallel execution. If you truly want parallel execution, you can use
the executeOnExecutor(java.util.concurrent.Executor, Params...) version of this method
with THREAD_POOL_EXECUTOR; however, see commentary there for warnings on
its use.
This method must be invoked on the UI thread.
params - The parameters of the task.java.lang.IllegalStateException - If getStatus() returns either
AsyncTask.Status#RUNNING or AsyncTask.Status#FINISHED.public final OptionalExecutorTask<Params,Progress,Result> executeOnExecutor(java.util.concurrent.Executor exec, Params... params)
This method is typically used with THREAD_POOL_EXECUTOR to
allow multiple tasks to run in parallel on a pool of threads managed by
AsyncTask, however you can also use your own Executor for custom
behavior.
Warning: Allowing multiple tasks to run in parallel from
a thread pool is generally not what one wants, because the order
of their operation is not defined. For example, if these tasks are used
to modify any state in common (such as writing a file due to a button click),
there are no guarantees on the order of the modifications.
Without careful work it is possible in rare cases for the newer version
of the data to be over-written by an older one, leading to obscure data
loss and stability issues. Such changes are best
executed in serial; to guarantee such work is serialized regardless of
platform version you can use this function with #SERIAL_EXECUTOR.
This method must be invoked on the UI thread.
exec - The executor to use. THREAD_POOL_EXECUTOR is available as a
convenient process-wide thread pool for tasks that are loosely coupled.params - The parameters of the task.java.lang.IllegalStateException - If getStatus() returns either
AsyncTask.Status#RUNNING or AsyncTask.Status#FINISHED.public static void execute(java.lang.Runnable runnable)
execute(Object...) for use with
a simple Runnable object.protected final void publishProgress(Progress... values)
doInBackground(Params...) to
publish updates on the UI thread while the background computation is
still running. Each call to this method will trigger the execution of
onProgressUpdate(Progress...) on the UI thread.
onProgressUpdate(Progress...) will note be called if the task has been
canceled.values - The progress values to update the UI with.onProgressUpdate(Progress...),
doInBackground(Params...)