com.google.common.util.concurrent
Class AbstractFuture<V>

java.lang.Object
  extended by com.google.common.util.concurrent.AbstractFuture<V>
All Implemented Interfaces:
Future<V>
Direct Known Subclasses:
AbstractListenableFuture

@Beta
public abstract class AbstractFuture<V>
extends Object
implements Future<V>

An abstract implementation of the Future interface. This class is an abstraction of FutureTask to support use for tasks other than Runnables. It uses an AbstractQueuedSynchronizer to deal with concurrency issues and guarantee thread safety. It could be used as a base class to FutureTask, or any other implementor of the Future interface.

This class implements all methods in Future. Subclasses should provide a way to set the result of the computation through the protected methods set(Object), setException(Throwable), or cancel(). If subclasses want to implement cancellation they can override the cancel(boolean) method with a real implementation, the default implementation doesn't support cancellation.

The state changing methods all return a boolean indicating success or failure in changing the future's state. Valid states are running, completed, failed, or cancelled. Because this class does not implement cancellation it is left to the subclass to distinguish between created and running tasks.

Since:
1
Author:
Sven Mawson

Constructor Summary
AbstractFuture()
           
 
Method Summary
protected  boolean cancel()
          Subclasses should invoke this method to mark the future as cancelled.
 boolean cancel(boolean mayInterruptIfRunning)
          Attempts to cancel execution of this task.
protected  void done()
           
 V get()
          Waits if necessary for the computation to complete, and then retrieves its result.
 V get(long timeout, TimeUnit unit)
          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()
          Returns true if this task completed.
protected  boolean set(V value)
          Subclasses should invoke this method to set the result of the computation to value.
protected  boolean setException(Throwable throwable)
          Subclasses should invoke this method to set the result of the computation to an error, throwable.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractFuture

public AbstractFuture()
Method Detail

get

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

Specified by:
get in interface Future<V>
Parameters:
timeout - the maximum time to wait
unit - the time unit of the timeout argument
Returns:
the computed result
Throws:
InterruptedException - if the current thread was interrupted while waiting
TimeoutException - if the wait timed out
ExecutionException - if the computation threw an exception

get

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

Specified by:
get in interface Future<V>
Returns:
the computed result
Throws:
InterruptedException - if the current thread was interrupted while waiting
ExecutionException - if the computation threw an exception

isDone

public boolean isDone()
Description copied from interface: java.util.concurrent.Future
Returns true if this task completed. Completion may be due to normal termination, an exception, or cancellation -- in all of these cases, this method will return true.

Specified by:
isDone in interface Future<V>
Returns:
true if this task completed

isCancelled

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

Specified by:
isCancelled in interface Future<V>
Returns:
true if this task was cancelled before it completed

cancel

public boolean cancel(boolean mayInterruptIfRunning)
Description copied from interface: java.util.concurrent.Future
Attempts to cancel execution of this task. This attempt will fail if the task has already completed, has 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 should never run. If the task has already started, then the mayInterruptIfRunning parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.

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.

Specified by:
cancel in interface Future<V>
Parameters:
mayInterruptIfRunning - true if the thread executing this task should be interrupted; otherwise, in-progress tasks are allowed to complete
Returns:
false if the task could not be cancelled, typically because it has already completed normally; true otherwise

set

protected boolean set(@Nullable
                      V value)
Subclasses should invoke this method to set the result of the computation to value. This will set the state of the future to AbstractFuture.Sync.COMPLETED and call done() if the state was successfully changed.

Parameters:
value - the value that was the result of the task.
Returns:
true if the state was successfully changed.

setException

protected boolean setException(Throwable throwable)
Subclasses should invoke this method to set the result of the computation to an error, throwable. This will set the state of the future to AbstractFuture.Sync.COMPLETED and call done() if the state was successfully changed.

Parameters:
throwable - the exception that the task failed with.
Returns:
true if the state was successfully changed.
Throws:
Error - if the throwable was an Error.

cancel

protected final boolean cancel()
Subclasses should invoke this method to mark the future as cancelled. This will set the state of the future to AbstractFuture.Sync.CANCELLED and call done() if the state was successfully changed.

Returns:
true if the state was successfully changed.

done

protected void done()