JBoss Threads 2.0.0.GA-redhat-2

org.jboss.threads
Class AsyncFutureTask<T>

java.lang.Object
  extended by org.jboss.threads.AsyncFutureTask<T>
All Implemented Interfaces:
Future<T>, AsyncCancellable, AsyncFuture<T>

public abstract class AsyncFutureTask<T>
extends Object
implements AsyncFuture<T>

A base class for implementing asynchronous tasks. This class implements Future as well as AsyncFuture, and is approximately equivalent to FutureTask, however it does not implement Runnable and is somewhat more flexible.

Author:
David M. Lloyd

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.jboss.threads.AsyncFuture
AsyncFuture.AbstractListener<T,A>, AsyncFuture.Listener<T,A>, AsyncFuture.Status
 
Constructor Summary
protected AsyncFutureTask(Executor executor)
          Construct a new instance.
 
Method Summary
<A> void
addListener(AsyncFuture.Listener<? super T,A> listener, A attachment)
          Add an asynchronous listener to be called when this operation completes.
 void asyncCancel(boolean interruptionDesired)
          Cancel this task.
 AsyncFuture.Status await()
          Wait if necessary for this operation to complete, returning the outcome.
 AsyncFuture.Status await(long timeout, TimeUnit unit)
          Wait if necessary for this operation to complete, returning the outcome, which may include AsyncFuture.Status.WAITING if the timeout expires before the operation completes.
 AsyncFuture.Status awaitUninterruptibly()
          Wait (uninterruptibly) if necessary for this operation to complete, returning the outcome.
 AsyncFuture.Status awaitUninterruptibly(long timeout, TimeUnit unit)
          Wait if necessary for this operation to complete, returning the outcome, which may include AsyncFuture.Status.WAITING if the timeout expires before the operation completes.
 boolean cancel(boolean interruptionDesired)
          Synchronously cancel a task, blocking uninterruptibly until it is known whether such cancellation was successful.
 T get()
          
 T get(long timeout, TimeUnit unit)
          
 AsyncFuture.Status getStatus()
          Get (poll) the current status of the asynchronous operation.
 T getUninterruptibly()
          Waits (uninterruptibly) if necessary for the computation to complete, and then retrieves the result.
 T getUninterruptibly(long timeout, TimeUnit unit)
          Waits (uninterruptibly) if necessary for at most the given time for the computation to complete, and then retrieves the result, if available.
 boolean isCancelled()
          
 boolean isDone()
          
protected  boolean setCancelled()
          Set the cancelled result of this operation.
protected  boolean setFailed(Throwable cause)
          Set the failure result of this operation.
protected  boolean setResult(T result)
          Set the successful result of this operation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AsyncFutureTask

protected AsyncFutureTask(Executor executor)
Construct a new instance.

Parameters:
executor - the executor to use for asynchronous notifications
Method Detail

setResult

protected final boolean setResult(T result)
Set the successful result of this operation. Once a result is set, calls to this or the other set*() methods are ignored.

Parameters:
result - the result
Returns:
true if the result was successfully set, or false if a result was already set

setCancelled

protected final boolean setCancelled()
Set the cancelled result of this operation. Once a result is set, calls to this or the other set*() methods are ignored.

Returns:
true if the result was successfully set, or false if a result was already set

setFailed

protected final boolean setFailed(Throwable cause)
Set the failure result of this operation. Once a result is set, calls to this or the other set*() methods are ignored.

Parameters:
cause - the cause of failure
Returns:
true if the result was successfully set, or false if a result was already set

asyncCancel

public void asyncCancel(boolean interruptionDesired)
Cancel this task. The default implementation of this method does nothing; if the task support asynchronous cancel, this method may be overridden to implement it. The implementation may choose to support or disregard the interruptionDesired flag. Implementations are allowed to interrupt threads associated with tasks even if the flag is false; likewise, implementations may choose not to interrupt threads even if the flag is true.

Specified by:
asyncCancel in interface AsyncCancellable
Specified by:
asyncCancel in interface AsyncFuture<T>
Parameters:
interruptionDesired - true if interruption of threads is desired

await

public final AsyncFuture.Status await()
                               throws InterruptedException
Wait if necessary for this operation to complete, returning the outcome. The outcome will be one of AsyncFuture.Status.COMPLETE, AsyncFuture.Status.CANCELLED, or AsyncFuture.Status.FAILED.

Specified by:
await in interface AsyncFuture<T>
Returns:
the outcome
Throws:
InterruptedException - if execution was interrupted while waiting

await

public final AsyncFuture.Status await(long timeout,
                                      TimeUnit unit)
                               throws InterruptedException
Wait if necessary for this operation to complete, returning the outcome, which may include AsyncFuture.Status.WAITING if the timeout expires before the operation completes.

Specified by:
await in interface AsyncFuture<T>
Parameters:
timeout - the maximum time to wait
unit - the time unit of the timeout argument
Returns:
the outcome
Throws:
InterruptedException - if execution was interrupted while waiting

awaitUninterruptibly

public final AsyncFuture.Status awaitUninterruptibly()
Wait (uninterruptibly) if necessary for this operation to complete, returning the outcome. The outcome will be one of AsyncFuture.Status.COMPLETE, AsyncFuture.Status.CANCELLED, or AsyncFuture.Status.FAILED.

Specified by:
awaitUninterruptibly in interface AsyncFuture<T>
Returns:
the outcome

awaitUninterruptibly

public final AsyncFuture.Status awaitUninterruptibly(long timeout,
                                                     TimeUnit unit)
Wait if necessary for this operation to complete, returning the outcome, which may include AsyncFuture.Status.WAITING if the timeout expires before the operation completes.

Specified by:
awaitUninterruptibly in interface AsyncFuture<T>
Parameters:
timeout - the maximum time to wait
unit - the time unit of the timeout argument
Returns:
the outcome

get

public final T get()
            throws InterruptedException,
                   ExecutionException

Specified by:
get in interface Future<T>
Throws:
InterruptedException
ExecutionException

get

public final T get(long timeout,
                   TimeUnit unit)
            throws InterruptedException,
                   ExecutionException,
                   TimeoutException

Specified by:
get in interface Future<T>
Throws:
InterruptedException
ExecutionException
TimeoutException

getUninterruptibly

public final T getUninterruptibly()
                           throws CancellationException,
                                  ExecutionException
Waits (uninterruptibly) if necessary for the computation to complete, and then retrieves the result.

Specified by:
getUninterruptibly in interface AsyncFuture<T>
Returns:
the computed result
Throws:
CancellationException - if the computation was cancelled
ExecutionException - if the computation threw an exception

getUninterruptibly

public final T getUninterruptibly(long timeout,
                                  TimeUnit unit)
                           throws CancellationException,
                                  ExecutionException,
                                  TimeoutException
Waits (uninterruptibly) if necessary for at most the given time for the computation to complete, and then retrieves the result, if available.

Specified by:
getUninterruptibly in interface AsyncFuture<T>
Parameters:
timeout - the maximum time to wait
unit - the time unit of the timeout argument
Returns:
the computed result
Throws:
CancellationException - if the computation was cancelled
ExecutionException - if the computation threw an exception
TimeoutException - if the wait timed out

getStatus

public final AsyncFuture.Status getStatus()
Get (poll) the current status of the asynchronous operation.

Specified by:
getStatus in interface AsyncFuture<T>
Returns:
the current status

addListener

public final <A> void addListener(AsyncFuture.Listener<? super T,A> listener,
                                  A attachment)
Add an asynchronous listener to be called when this operation completes.

Specified by:
addListener in interface AsyncFuture<T>
Type Parameters:
A - the attachment type
Parameters:
listener - the listener to add
attachment - the attachment to pass in

cancel

public final boolean cancel(boolean interruptionDesired)
Synchronously cancel a task, blocking uninterruptibly until it is known whether such cancellation was successful. Note that the Future.cancel(boolean) is somewhat unclear about blocking semantics. It is recommended to use AsyncFuture.asyncCancel(boolean) instead.

Specified by:
cancel in interface Future<T>
Specified by:
cancel in interface AsyncFuture<T>
Parameters:
interruptionDesired - if interruption is desired (if available)
Returns:
true if cancel succeeded, false otherwise

isCancelled

public final boolean isCancelled()

Specified by:
isCancelled in interface Future<T>

isDone

public final boolean isDone()

Specified by:
isDone in interface Future<T>

JBoss Threads 2.0.0.GA-redhat-2

Copyright © 2012 JBoss by Red Hat. All Rights Reserved.