public abstract class AsyncTask
extends java.lang.Object
implements java.lang.Runnable
AsyncTask
is a part of a basic API to allow
scheduling asynchronous tasks that support cancelling while the
task is running. It is recommended that JSR 166 be used when
it becomes available as part of the JDK later. The process of notifying a running task that a cancellation has been requested (and it should abort) is implementation-specific. For example, the following is a possible implementation:
private boolean _isCancelled = false; protected void requestCancelImpl() { _isCancelled = true; } protected boolean isCancelledImpl() { return _isCancelled; }
Within runImpl()
, the implementation could then
call bailIfCancelled()
periodically to bail out
of the task if it has been marked for cancellation. Note, it is
the implementation's responsibility to ensure that any acquired
locks or resources are properly released in case of task
cancellation.
Modifier and Type | Class and Description |
---|---|
static class |
AsyncTask.CancelledException
The
CancelledException is used to bail out of a
running task. |
Modifier and Type | Field and Description |
---|---|
protected static java.lang.String |
CANCELLED_RESULT
Tasks that are cancelled successfully in-progress should return
this cancelled result, so that the task status can be updated
accordingly.
|
static int |
STATUS_CANCELLED
Constant for cancelled status.
|
static int |
STATUS_COMPLETED
Constant for completed successfully status.
|
static int |
STATUS_FAILED
Constant for completed w/ failed status due to RuntimeException.
|
static int |
STATUS_READY
Constant for ready-to-run status.
|
static int |
STATUS_RUNNING
Constant for currently running status.
|
Modifier | Constructor and Description |
---|---|
protected |
AsyncTask()
Set up the AsyncTask in ready-to-run mode.
|
Modifier and Type | Method and Description |
---|---|
protected void |
bailIfCancelled()
Checks if the task has been marked for cancellation, and throws
a CancelledException if it has.
|
java.lang.Object |
getResult()
Fetch the completed result of the task (if any).
|
int |
getStatus()
Fetches the current status of the task.
|
protected abstract boolean |
isCancelledImpl()
Return whether the task is marked for cancellation, to be implemented
by the implementation.
|
boolean |
isFinished()
Fetch whether this task is completed or not.
|
protected void |
requestCancel()
Request to cancel the task.
|
protected abstract void |
requestCancelImpl()
Request to cancel the task in-progress, to be implemented by
the implementation.
|
void |
run()
Performs the task.
|
protected abstract java.lang.Object |
runImpl()
Implementation of the task.
|
protected static void |
sleep(int ms)
Utility routine to sleep for the given amount of time.
|
void |
waitUntilFinished()
Waits until the task is finished (i.e., isFinished() returns true).
|
void |
waitUntilFinished(int ms)
Waits until the task is finished (i.e., isFinished() returns true).
|
public static final int STATUS_READY
public static final int STATUS_RUNNING
public static final int STATUS_COMPLETED
public static final int STATUS_FAILED
public static final int STATUS_CANCELLED
protected static final java.lang.String CANCELLED_RESULT
public final int getStatus()
public boolean isFinished()
public void waitUntilFinished()
public void waitUntilFinished(int ms)
ms
- the sleep granularity to useprotected static void sleep(int ms)
ms
- the time in ms to sleeppublic final void run()
run
in interface java.lang.Runnable
protected abstract java.lang.Object runImpl() throws AsyncTask.CancelledException
AsyncTask.CancelledException
- if the task has been cancelledprotected abstract void requestCancelImpl()
protected abstract boolean isCancelledImpl()
protected final void requestCancel()
protected void bailIfCancelled() throws AsyncTask.CancelledException
AsyncTask.CancelledException
- if the task has been marked for cancellationpublic final java.lang.Object getResult()