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

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:
2009.09.15 tentative
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)
           
protected  void done()
           
 V get()
           
 V get(long timeout, TimeUnit unit)
           
 boolean isCancelled()
           
 boolean isDone()
           
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
Specified by:
get in interface Future<V>
Throws:
InterruptedException
TimeoutException
ExecutionException

get

public V get()
      throws InterruptedException,
             ExecutionException
Specified by:
get in interface Future<V>
Throws:
InterruptedException
ExecutionException

isDone

public boolean isDone()
Specified by:
isDone in interface Future<V>

isCancelled

public boolean isCancelled()
Specified by:
isCancelled in interface Future<V>

cancel

public boolean cancel(boolean mayInterruptIfRunning)
Specified by:
cancel in interface Future<V>

set

protected boolean set(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()