pivot.util.concurrent
Class Task<V>

java.lang.Object
  extended by pivot.util.concurrent.Task<V>
Direct Known Subclasses:
IOTask, TaskGroup, TaskSequence

public abstract class Task<V>
extends java.lang.Object

Abstract base class for "tasks". A task is an asynchronous operation that may optionally return a value.

Author:
gbrown

Field Summary
protected  boolean abort
           
protected  long timeout
           
 
Constructor Summary
Task()
           
Task(Dispatcher dispatcher)
           
 
Method Summary
 void abort()
          Sets the abort flag for this task to true.
abstract  V execute()
          Synchronously executes the task.
 void execute(TaskListener<V> taskListener)
          Asynchronously executes the task.
 Dispatcher getDispatcher()
          Returns the dispatcher used to execute this task.
 java.lang.Exception getFault()
          Returns the fault that occurred while executing the task.
 V getResult()
          Returns the result of executing the task.
 long getTimeout()
          Returns the timeout value for this task.
 boolean isPending()
          Returns the pending state of the task.
 void setTimeout(long timeout)
          Sets the timeout value for this task.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

timeout

protected volatile long timeout

abort

protected volatile boolean abort
Constructor Detail

Task

public Task()

Task

public Task(Dispatcher dispatcher)
Method Detail

execute

public abstract V execute()
                   throws TaskExecutionException
Synchronously executes the task.

Returns:
The result of the task's execution.
Throws:
TaskExecutionException - If an error occurs while executing the task.

execute

public void execute(TaskListener<V> taskListener)
Asynchronously executes the task. The caller is notified of the task's completion via the listener argument. Note that the listener will be notified on the task's worker thread, not on the thread that executed the task.

Parameters:
taskListener - The listener to be notified when the task completes.

getDispatcher

public Dispatcher getDispatcher()
Returns the dispatcher used to execute this task.


getResult

public V getResult()
Returns the result of executing the task.

Returns:
The task result, or null if the task is still executing or has failed. The result itself may also be null; callers should call isPending() and getFault() to distinguish between these cases.

getFault

public java.lang.Exception getFault()
Returns the fault that occurred while executing the task.

Returns:
The task fault, or null if the task is still executing or has succeeded. Callers should call isPending() to distinguish between these cases.

isPending

public boolean isPending()
Returns the pending state of the task.

Returns:
true if the task is awaiting execution or currently executing; false, otherwise.

getTimeout

public long getTimeout()
Returns the timeout value for this task.

See Also:
setTimeout(long)

setTimeout

public void setTimeout(long timeout)
Sets the timeout value for this task. It is the responsibility of the implementing class to respect this value.

Parameters:
timeout - The time by which the task must complete execution. If the timeout is exceeded, a TimeoutException will be thrown.

abort

public void abort()
Sets the abort flag for this task to true. It is the responsibility of the implementing class to respect this value and throw a AbortException.