java.util.concurrent
Class FutureTask<V>

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.util.concurrent.FutureTask<V>
Type Parameters:
V - The result type returned by this FutureTask's get method
All Implemented Interfaces:
Runnable sample code for java.lang.Runnable definition code for java.lang.Runnable , Future sample code for java.util.concurrent.Future definition code for java.util.concurrent.Future <V>

public class FutureTask<V>
extends Object sample code for java.lang.Object definition code for java.lang.Object
implements Future sample code for java.util.concurrent.Future definition code for java.util.concurrent.Future <V>, Runnable sample code for java.lang.Runnable definition code for java.lang.Runnable

A cancellable asynchronous computation. This class provides a base implementation of Future sample code for java.util.concurrent.Future definition code for java.util.concurrent.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 sample code for java.util.concurrent.Callable definition code for java.util.concurrent.Callable or Runnable sample code for java.lang.Runnable definition code for java.lang.Runnable object. Because FutureTask implements Runnable, a FutureTask can be submitted to an Executor sample code for java.util.concurrent.Executor definition code for java.util.concurrent.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

Constructor Summary
FutureTask sample code for java.util.concurrent.FutureTask.FutureTask(java.util.concurrent.Callable) definition code for java.util.concurrent.FutureTask.FutureTask(java.util.concurrent.Callable) (Callable sample code for java.util.concurrent.Callable definition code for java.util.concurrent.Callable <V> callable)
          Creates a FutureTask that will upon running, execute the given Callable.
FutureTask sample code for java.util.concurrent.FutureTask.FutureTask(java.lang.Runnable, V) definition code for java.util.concurrent.FutureTask.FutureTask(java.lang.Runnable, V) (Runnable sample code for java.lang.Runnable definition code for java.lang.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.
 
Method Summary
 boolean cancel sample code for java.util.concurrent.FutureTask.cancel(boolean) definition code for java.util.concurrent.FutureTask.cancel(boolean) (boolean mayInterruptIfRunning)
          Attempts to cancel execution of this task.
protected  void done sample code for java.util.concurrent.FutureTask.done() definition code for java.util.concurrent.FutureTask.done() ()
          Protected method invoked when this task transitions to state isDone (whether normally or via cancellation).
 V get sample code for java.util.concurrent.FutureTask.get() definition code for java.util.concurrent.FutureTask.get() ()
          Waits if necessary for the computation to complete, and then retrieves its result.
 V get sample code for java.util.concurrent.FutureTask.get(long, java.util.concurrent.TimeUnit) definition code for java.util.concurrent.FutureTask.get(long, java.util.concurrent.TimeUnit) (long timeout, TimeUnit sample code for java.util.concurrent.TimeUnit definition code for java.util.concurrent.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 sample code for java.util.concurrent.FutureTask.isCancelled() definition code for java.util.concurrent.FutureTask.isCancelled() ()
          Returns true if this task was cancelled before it completed normally.
 boolean isDone sample code for java.util.concurrent.FutureTask.isDone() definition code for java.util.concurrent.FutureTask.isDone() ()
          Returns true if this task completed.
 void run sample code for java.util.concurrent.FutureTask.run() definition code for java.util.concurrent.FutureTask.run() ()
          Sets this Future to the result of computation unless it has been cancelled.
protected  boolean runAndReset sample code for java.util.concurrent.FutureTask.runAndReset() definition code for java.util.concurrent.FutureTask.runAndReset() ()
          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.
protected  void set sample code for java.util.concurrent.FutureTask.set(V) definition code for java.util.concurrent.FutureTask.set(V) (V v)
          Sets the result of this Future to the given value unless this future has already been set or has been cancelled.
protected  void setException sample code for java.util.concurrent.FutureTask.setException(java.lang.Throwable) definition code for java.util.concurrent.FutureTask.setException(java.lang.Throwable) (Throwable sample code for java.lang.Throwable definition code for 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.
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Constructor Detail

FutureTask sample code for java.util.concurrent.FutureTask(java.util.concurrent.Callable<V>) definition code for java.util.concurrent.FutureTask(java.util.concurrent.Callable<V>)

public FutureTask(Callable sample code for java.util.concurrent.Callable definition code for java.util.concurrent.Callable <V> callable)
Creates a FutureTask that will upon running, execute the given Callable.

Parameters:
callable - the callable task
Throws:
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if callable is null

FutureTask sample code for java.util.concurrent.FutureTask(java.lang.Runnable, V) definition code for java.util.concurrent.FutureTask(java.lang.Runnable, V)

public FutureTask(Runnable sample code for java.lang.Runnable definition code for java.lang.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.

Parameters:
runnable - the runnable task
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 sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if runnable is null
Method Detail

isCancelled sample code for java.util.concurrent.FutureTask.isCancelled() definition code for java.util.concurrent.FutureTask.isCancelled()

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

Specified by:
isCancelled sample code for java.util.concurrent.Future.isCancelled() definition code for java.util.concurrent.Future.isCancelled() in interface Future sample code for java.util.concurrent.Future definition code for java.util.concurrent.Future <V>
Returns:
true if task was cancelled before it completed

isDone sample code for java.util.concurrent.FutureTask.isDone() definition code for java.util.concurrent.FutureTask.isDone()

public boolean isDone()
Description copied from interface: Future sample code for java.util.concurrent.Future.isDone() definition code for java.util.concurrent.Future.isDone()
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 sample code for java.util.concurrent.Future.isDone() definition code for java.util.concurrent.Future.isDone() in interface Future sample code for java.util.concurrent.Future definition code for java.util.concurrent.Future <V>
Returns:
true if this task completed.

cancel sample code for java.util.concurrent.FutureTask.cancel(boolean) definition code for java.util.concurrent.FutureTask.cancel(boolean)

public boolean cancel(boolean mayInterruptIfRunning)
Description copied from interface: Future sample code for java.util.concurrent.Future.cancel(boolean) definition code for java.util.concurrent.Future.cancel(boolean)
Attempts to cancel execution of this task. This attempt will fail if the task has already completed, 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.

Specified by:
cancel sample code for java.util.concurrent.Future.cancel(boolean) definition code for java.util.concurrent.Future.cancel(boolean) in interface Future sample code for java.util.concurrent.Future definition code for java.util.concurrent.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

get sample code for java.util.concurrent.FutureTask.get() definition code for java.util.concurrent.FutureTask.get()

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

Specified by:
get sample code for java.util.concurrent.Future.get() definition code for java.util.concurrent.Future.get() in interface Future sample code for java.util.concurrent.Future definition code for java.util.concurrent.Future <V>
Returns:
the computed result
Throws:
InterruptedException sample code for java.lang.InterruptedException definition code for java.lang.InterruptedException - if the current thread was interrupted while waiting
ExecutionException sample code for java.util.concurrent.ExecutionException definition code for java.util.concurrent.ExecutionException - if the computation threw an exception

get sample code for java.util.concurrent.FutureTask.get(long, java.util.concurrent.TimeUnit) definition code for java.util.concurrent.FutureTask.get(long, java.util.concurrent.TimeUnit)

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

Specified by:
get sample code for java.util.concurrent.Future.get(long, java.util.concurrent.TimeUnit) definition code for java.util.concurrent.Future.get(long, java.util.concurrent.TimeUnit) in interface Future sample code for java.util.concurrent.Future definition code for java.util.concurrent.Future <V>
Parameters:
timeout - the maximum time to wait
unit - the time unit of the timeout argument
Returns:
the computed result
Throws:
InterruptedException sample code for java.lang.InterruptedException definition code for java.lang.InterruptedException - if the current thread was interrupted while waiting
ExecutionException sample code for java.util.concurrent.ExecutionException definition code for java.util.concurrent.ExecutionException - if the computation threw an exception
TimeoutException sample code for java.util.concurrent.TimeoutException definition code for java.util.concurrent.TimeoutException - if the wait timed out

done sample code for java.util.concurrent.FutureTask.done() definition code for java.util.concurrent.FutureTask.done()

protected void done()
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.


set sample code for java.util.concurrent.FutureTask.set(V) definition code for java.util.concurrent.FutureTask.set(V)

protected void set(V v)
Sets the result of this Future to the given value unless this future has already been set or has been cancelled.

Parameters:
v - the value

setException sample code for java.util.concurrent.FutureTask.setException(java.lang.Throwable) definition code for java.util.concurrent.FutureTask.setException(java.lang.Throwable)

protected void setException(Throwable sample code for java.lang.Throwable definition code for 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.

Parameters:
t - the cause of failure.

run sample code for java.util.concurrent.FutureTask.run() definition code for java.util.concurrent.FutureTask.run()

public void run()
Sets this Future to the result of computation unless it has been cancelled.

Specified by:
run sample code for java.lang.Runnable.run() definition code for java.lang.Runnable.run() in interface Runnable sample code for java.lang.Runnable definition code for java.lang.Runnable
See Also:
Thread.run() sample code for java.lang.Thread.run() definition code for java.lang.Thread.run()

runAndReset sample code for java.util.concurrent.FutureTask.runAndReset() definition code for java.util.concurrent.FutureTask.runAndReset()

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

Returns:
true if successfully run and reset