|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectedu.emory.mathcs.util.concurrent.AsyncTask
A class maintaining a single reference variable serving as the result of an operation. The result cannot be accessed until it has been set.
This class is intended primarily for subclassing. Typical usage scenario
is to create a new instance and invoke
createPerformer
, thus
obtaining runnable that will execute specified task and set results in
that instance. Note that such obtained runnable should be executed only
once -- subsequent execution attempts will fail due to "task already
completed" condition.
Executor
Nested Class Summary | |
static interface |
AsyncTask.Cancellable
|
Field Summary | |
protected AsyncTask.Cancellable |
cancellationHandler
Wrapper for the thread in which async task is executed, or the task itself if it implements AsyncTask.Cancellable . |
Constructor Summary | |
protected |
AsyncTask()
|
protected |
AsyncTask(Callback cb)
|
Method Summary | |
boolean |
cancel(boolean mayInterruptIfRunning)
Attempts to cancel execution of this task. |
protected AsyncTask.Cancellable |
createCancellationHandler(Callable call)
Overridable cancellation policy that governs what should be done upon cancellation of tasks that have already started running. |
protected java.lang.Runnable |
createPerformer(Callable call,
boolean disableStackTraces)
Creates a runnable that will execute specified call and then mark this AsyncTask with the result of that call. |
java.lang.Object |
get()
Waits if necessary for the computation to complete, and then retrieves its result. |
java.lang.Object |
get(long timeout,
TimeUnit tunit)
Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available. |
boolean |
isCancelled()
Returns true if this task was cancelled before it completed normally. |
boolean |
isDone()
Checks if the task has completed. |
protected void |
setCompleted(java.lang.Object result)
Marks the task as completed. |
protected void |
setFailed(java.lang.Throwable exception)
Marks the task as failed. |
static AsyncTask |
start(Executor executor,
Callable call)
Schedules specified task with given executor, and returns completion handle that can be used to access the result or to cancel the task. |
static AsyncTask |
start(Executor executor,
Callable call,
Callback cb)
Schedules specified task with given executor, and returns completion handle that can be used to access the result or to cancel the task. |
static AsyncTask |
start(Executor executor,
Callable call,
Callback cb,
boolean disableStackTraces)
Schedules specified task with given executor, and returns completion handle that can be used to access the result or to cancel the task. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected AsyncTask.Cancellable cancellationHandler
AsyncTask.Cancellable
. Set by the thread that
is just about to start executing the task.
Constructor Detail |
protected AsyncTask()
protected AsyncTask(Callback cb)
Method Detail |
public boolean isDone()
isDone
in interface Future
public boolean cancel(boolean mayInterruptIfRunning)
Future
After this method returns, subsequent calls to Future.isDone()
will
always return true. Subsequent calls to Future.isCancelled()
will always return true if this method returned true.
cancel
in interface Future
mayInterruptIfRunning
- true if the thread executing this
task should be interrupted; otherwise, in-progress tasks are allowed
to complete
public boolean isCancelled()
Future
isCancelled
in interface Future
protected void setCompleted(java.lang.Object result)
result
- the result of a task.protected void setFailed(java.lang.Throwable exception)
exception
- the cause of abrupt completion.
java.lang.IllegalStateException
- if task had been completed already.public java.lang.Object get() throws java.lang.InterruptedException, ExecutionException
Future
get
in interface Future
ExecutionException
- if the computation threw an
exception
java.lang.InterruptedException
- if the current thread was interrupted
while waitingpublic java.lang.Object get(long timeout, TimeUnit tunit) throws java.lang.InterruptedException, ExecutionException, TimeoutException
Future
get
in interface Future
timeout
- the maximum time to waittunit
- the time unit of the timeout argument
java.lang.InterruptedException
- if the current thread was interrupted
while waiting
ExecutionException
- if the computation threw an
exception
TimeoutException
- if the wait timed outpublic static AsyncTask start(Executor executor, Callable call)
executor
- the executor to usecall
- the task to schedule
public static AsyncTask start(Executor executor, Callable call, Callback cb)
executor
- the executor to usecall
- the task to schedulecb
- callback to invoke upon completion
public static AsyncTask start(Executor executor, Callable call, Callback cb, boolean disableStackTraces)
executor
- the executor to usecall
- the task to schedulecb
- callback to invoke upon completiondisableStackTraces
- if true, does not append invoker stack trace
to traces of exceptions thrown during task execution
protected java.lang.Runnable createPerformer(Callable call, boolean disableStackTraces)
This method is intended to be used by subclasses. Runnable returned from this method should be executed only once -- subsequent execution attempts will fail due to "task already completed" condition.
call
- the call to executedisableStackTraces
- if true, does not append invoker stack trace
to traces of exceptions thrown during execution of the runnable
protected AsyncTask.Cancellable createCancellationHandler(Callable call)
createPerformer
, before invoking the actual
call. The
cancellationHandler returned from this method is then stored in this
AsyncTask. Later, if the user attempts cancellation while the call is
already executing, the request is delegated to the handler which must
then supply the appropriate action and indication of success or failure.
The default implementation behaves as follows. If the callable for
which the cancellation handler is requested implements
AsyncTask.Cancellable
itself, that callable itself is returned as its own
cancellation handler; in other words, the cancellation policy will be
supplied directly by the callable implementation. Otherwise, the
default behavior is to interrupt the worker thread if the
mayInterruptIfRunning parameter is set to true, and fail in the other
case.
call
- the call for which the cancellation handler is requested
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |