java.util.concurrent
Class FutureTask<V>

java.lang.Object
  extended byjava.util.concurrent.CancellableTask
      extended byjava.util.concurrent.FutureTask
All Implemented Interfaces:
Cancellable, Future, java.lang.Runnable

public class FutureTask<V>
extends CancellableTask
implements Future<V>

A cancellable asynchronous computation. This class provides a base implementation of Future, with methods to start and cancel a computation, query to see if the computation is complete, and retrieve the result of the computation. The result can only be retrieved when the computation has completed; the get method will block if the computation has not yet completed. Once the computation has completed, the computation cannot be restarted or cancelled.

A FutureTask can be used to wrap a Callable or Runnable object. Because FutureTask implements Runnable, a FutureTask can be submitted to an Executor for execution.

Since:
1.5
Author:
Doug Lea

Nested Class Summary
 
Nested classes inherited from class java.util.concurrent.CancellableTask
CancellableTask.InnerCancellableFuture<V>
 
Constructor Summary
FutureTask(Callable<V> callable)
          Constructs a FutureTask that will upon running, execute the given Callable.
FutureTask(java.lang.Runnable runnable, V result)
          Constructs a FutureTask that will upon running, execute the given Runnable, and arrange that get will return the given result on successful completion.
 
Method Summary
 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.
protected  void set(V v)
          Sets the value of this task to the given value.
protected  void setException(java.lang.Throwable t)
          Indicates that the computation has failed.
 
Methods inherited from class java.util.concurrent.CancellableTask
cancel, getRunnable, isCancelled, isDone, reset, run, setCancelled, setDone, setRunnable, setRunning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.concurrent.Cancellable
cancel, isCancelled, isDone
 

Constructor Detail

FutureTask

public FutureTask(Callable<V> callable)
Constructs a FutureTask that will upon running, execute the given Callable.

Parameters:
callable - the callable task
Throws:
java.lang.NullPointerException - if callable is null

FutureTask

public FutureTask(java.lang.Runnable runnable,
                  V result)
Constructs a FutureTask that will upon running, execute the given Runnable, and arrange that get will return the given result on successful completion.

Parameters:
runnable - the runnable task
result - the result to return on successful completion. If you don't need a particular result, consider using Boolean.TRUE.
Throws:
java.lang.NullPointerException - if runnable is null
Method Detail

get

public V get()
      throws java.lang.InterruptedException,
             ExecutionException
Waits if necessary for the computation to complete, and then retrieves its result.

Specified by:
get in interface Future
Returns:
the computed result
Throws:
CancellationException - if task producing this value was cancelled before completion
ExecutionException - if the underlying computation threw an exception
java.lang.InterruptedException - if current thread was interrupted while waiting

get

public V get(long timeout,
             TimeUnit unit)
      throws java.lang.InterruptedException,
             ExecutionException,
             TimeoutException
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
Parameters:
timeout - the maximum time to wait
unit - the time unit of the timeout argument
Returns:
value of this task
Throws:
CancellationException - if task producing this value was cancelled before completion
ExecutionException - if the underlying computation threw an exception.
java.lang.InterruptedException - if current thread was interrupted while waiting
TimeoutException - if the wait timed out

set

protected void set(V v)
Sets the value of this task to the given value. After this method is called, the computation is assumed to be completed -- threads already waiting for the result via get are unblocked, and future attempts to retrieve the result will not block. While not explicitly disallowed, it is rarely a good idea to invoke set more than once.

Parameters:
v - the value

setException

protected void setException(java.lang.Throwable t)
Indicates that the computation has failed. After this method is called, the computation is assumed to be completed, and any attempt to retrieve the result will throw an ExecutionException wrapping the exception provided here.

Parameters:
t - the throwable