Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

edu.emory.mathcs.util.concurrent
Class AsyncTask  view AsyncTask download AsyncTask.java

java.lang.Object
  extended byedu.emory.mathcs.util.concurrent.AsyncTask
All Implemented Interfaces:
Cancellable, Future
Direct Known Subclasses:
SecureAsyncTask, ThreadUtils.Executioner.Result

public class AsyncTask
extends java.lang.Object
implements Future, Cancellable

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 55 , 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.


Field Summary
(package private)  Callback callback
          Optional callback to invoke when task completes
(package private)  CancellationException cancellationException
           
protected  Cancellable cancellationHandler
          Wrapper for the thread in which async task is executed, or the task itself if it implements Cancellable.
(package private)  boolean completed
          completion status
(package private)  java.lang.Object result
          result of a task
(package private)  java.lang.Throwable resultException
          exception object in case task completed abruptly
 
Constructor Summary
protected AsyncTask()
           
protected AsyncTask(Callback cb)
           
 
Method Summary
private static void appendContextStackTrace(java.lang.Throwable ex, java.lang.Throwable cxt)
           
 boolean cancel(boolean mayInterruptIfRunning)
          Attempt to cancel execution of this task.
protected  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 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.
private  java.lang.Object getResult()
          Gets the result of the task.
private  void invokeCallback(java.lang.Object result, java.lang.Throwable resultException)
           
 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.
private  void waitFor()
          Waits for the task to complete.
private  void waitFor(long timeout)
          Waits for the task to complete for timeout milliseconds.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

completed

volatile boolean completed
completion status


cancellationException

CancellationException cancellationException

cancellationHandler

protected Cancellable cancellationHandler
Wrapper for the thread in which async task is executed, or the task itself if it implements Cancellable. Set by the thread that is just about to start executing the task.


callback

final Callback callback
Optional callback to invoke when task completes


result

java.lang.Object result
result of a task


resultException

java.lang.Throwable resultException
exception object in case task completed abruptly

Constructor Detail

AsyncTask

protected AsyncTask()

AsyncTask

protected AsyncTask(Callback cb)
Method Detail

isDone

public boolean isDone()
Checks if the task has completed. Not synchronized to improve concurrency, using "volatile" instead

Specified by:
isDone in interface Future

cancel

public boolean cancel(boolean mayInterruptIfRunning)
Description copied from interface: Cancellable
Attempt 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 will never run. If the task has already started, then the interruptIfRunning parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.

Specified by:
cancel in interface Cancellable

isCancelled

public boolean isCancelled()
Description copied from interface: Cancellable
Returns true if this task was cancelled before it completed normally.

Specified by:
isCancelled in interface Cancellable

setCompleted

protected void setCompleted(java.lang.Object result)
Marks the task as completed.


setFailed

protected void setFailed(java.lang.Throwable exception)
Marks the task as failed.


get

public java.lang.Object get()
                     throws java.lang.InterruptedException,
                            ExecutionException
Description copied from interface: Future
Waits if necessary for computation to complete, and then retrieves its result.

Specified by:
get in interface Future

get

public java.lang.Object get(long timeout,
                            TimeUnit tunit)
                     throws java.lang.InterruptedException,
                            ExecutionException,
                            TimeoutException
Description copied from interface: Future
Waits if necessary for at most the given time for the computation to complete, and then retrieves its result.

Specified by:
get in interface Future

waitFor

private void waitFor()
              throws java.lang.InterruptedException
Waits for the task to complete.


waitFor

private void waitFor(long timeout)
              throws java.lang.InterruptedException
Waits for the task to complete for timeout milliseconds.


getResult

private java.lang.Object getResult()
                            throws ExecutionException
Gets the result of the task. PRE: task completed PRE: called from synchronized block


start

public 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. Later, if task completes successfully, the handle is marked completed with the result that has been returned from the task. If the task ends with an exception, the handle is marked failed with cause being that exception. In such case, as a debugging aid, current stack trace (i.e. that of this method's invoker) is appended to the original stack trace.


start

public 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. Later, if task completes successfully, the handle is marked completed with the result that has been returned from the task. If the task ends with an exception, the handle is marked failed with cause being that exception. In such case, as a debugging aid, current stack trace (i.e. that of this method's invoker) is appended to the original stack trace.


start

public 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. Later, if task completes successfully, the handle is marked completed with the result that has been returned from the task. If the task ends with an exception, the handle is marked failed with cause being that exception. In such case, as a debugging aid, current stack trace (i.e. that of this method's invoker) is appended to the original stack trace unless the disableStackTraces parameter is set to false


createPerformer

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. If the call completes successfully, this AsyncTask is marked completed with the result returned by the call. If the call throws an exception, the AsyncTask is marked as failed with cause being that exception. The stack trace of the thread in which the performer is created is appended to the failure cause stack trace unless the disableStackTraces parameter is set to false.

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.


createCancellationHandler

protected Cancellable createCancellationHandler(Callable call)
Overridable cancellation policy that governs what should be done upon cancellation of tasks that have already started running. This method is invoked in the worker thread by the runnable created by createPerformer 55 , 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 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.


appendContextStackTrace

private static void appendContextStackTrace(java.lang.Throwable ex,
                                            java.lang.Throwable cxt)

invokeCallback

private void invokeCallback(java.lang.Object result,
                            java.lang.Throwable resultException)