FileDocCategorySizeDatePackage
FutureTask.javaAPI DocJava SE 5 API9358Fri Aug 26 14:57:26 BST 2005java.util.concurrent

FutureTask

public class FutureTask extends Object implements Runnable, Future
A cancellable asynchronous computation. This class provides a base implementation of {@link 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 {@link Callable} or {@link java.lang.Runnable} object. Because FutureTask implements Runnable, a FutureTask can be submitted to an {@link Executor} for execution.

In addition to serving as a standalone class, this class provides protected functionality that may be useful when creating customized task classes.

since
1.5
author
Doug Lea
param
The result type returned by this FutureTask's get method

Fields Summary
private final Sync
sync
Synchronization control for FutureTask
Constructors Summary
public FutureTask(Callable callable)
Creates a FutureTask that will upon running, execute the given Callable.

param
callable the callable task
throws
NullPointerException if callable is null

        if (callable == null)
            throw new NullPointerException();
        sync = new Sync(callable);
    
public FutureTask(Runnable runnable, V result)
Creates a FutureTask that will upon running, execute the given Runnable, and arrange that get will return the given result on successful completion.

param
runnable the runnable task
param
result the result to return on successful completion. If you don't need a particular result, consider using constructions of the form: Future<?> f = new FutureTask<Object>(runnable, null)
throws
NullPointerException if runnable is null

        sync = new Sync(Executors.callable(runnable, result));
    
Methods Summary
public booleancancel(boolean mayInterruptIfRunning)

        return sync.innerCancel(mayInterruptIfRunning);
    
protected voiddone()
Protected method invoked when this task transitions to state isDone (whether normally or via cancellation). The default implementation does nothing. Subclasses may override this method to invoke completion callbacks or perform bookkeeping. Note that you can query status inside the implementation of this method to determine whether this task has been cancelled.

 
public Vget()

        return sync.innerGet();
    
public Vget(long timeout, java.util.concurrent.TimeUnit unit)

        return sync.innerGet(unit.toNanos(timeout));
    
public booleanisCancelled()

        return sync.innerIsCancelled();
    
public booleanisDone()

        return sync.innerIsDone();
    
public voidrun()
Sets this Future to the result of computation unless it has been cancelled.

        sync.innerRun();
    
protected booleanrunAndReset()
Executes the computation without setting its result, and then resets this Future to initial state, failing to do so if the computation encounters an exception or is cancelled. This is designed for use with tasks that intrinsically execute more than once.

return
true if successfully run and reset

        return sync.innerRunAndReset();
    
protected voidset(V v)
Sets the result of this Future to the given value unless this future has already been set or has been cancelled.

param
v the value

        sync.innerSet(v);
    
protected voidsetException(java.lang.Throwable t)
Causes this future to report an ExecutionException with the given throwable as its cause, unless this Future has already been set or has been cancelled.

param
t the cause of failure.

        sync.innerSetException(t);