Class FutureTask<V>

java.lang.Object
java.util.concurrent.FutureTask<V>
All Implemented Interfaces:
Runnable, Future<V>, RunnableFuture<V>

public class FutureTask<V> extends Object implements RunnableFuture<V>

A cancellable computation whose result is read through Future.

The state lives under this object's monitor, so it may be run, cancelled and waited for from any number of threads. get() on the UI thread waits through JdkHost.blockUi, which keeps events being dispatched.

cancel(true) interrupts the thread running the task, as in the JDK.

  • Constructor Summary

    Constructors
    Constructor
    Description
    FutureTask(Runnable runnable, V result)
     
    FutureTask(Callable<V> callable)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    cancel(boolean mayInterruptIfRunning)
    Stops the work if it has not started.
    protected void
    Called once when the task becomes done, by whichever thread made it so: completion, failure or cancellation.
    get()
    Waits for the work to finish and returns its result.
    get(long timeout, TimeUnit unit)
    Waits up to timeout for the work to finish and returns its result.
    boolean
    Whether Future.cancel(boolean) cancelled the work before it ran.
    boolean
    Whether the work has finished: completed, failed or cancelled.
    void
    run()
    Computes the result and completes this future with it, unless it was cancelled.
    protected boolean
    Runs the task without setting a result, leaving the future ready to run again: for work that repeats.
    protected void
    set(V v)
    Completes the future with v, unless it is already done.
    protected void
    Fails the future with t, unless it is already done.
    Returns a string representation of the object.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • FutureTask

      public FutureTask(Callable<V> callable)
    • FutureTask

      public FutureTask(Runnable runnable, V result)
  • Method Details

    • cancel

      public boolean cancel(boolean mayInterruptIfRunning)
      Description copied from interface: Future
      Stops the work if it has not started. Answers whether it was cancelled; whether work already running is interrupted is up to the implementation and to mayInterruptIfRunning.
      Specified by:
      cancel in interface Future<V>
      Parameters:
      mayInterruptIfRunning - whether a running task may be interrupted
      Returns:
      true when this call cancelled it
    • isCancelled

      public boolean isCancelled()
      Description copied from interface: Future
      Whether Future.cancel(boolean) cancelled the work before it ran.
      Specified by:
      isCancelled in interface Future<V>
      Returns:
      true when cancelled
    • isDone

      public boolean isDone()
      Description copied from interface: Future
      Whether the work has finished: completed, failed or cancelled.
      Specified by:
      isDone in interface Future<V>
      Returns:
      true once it will not change again
    • done

      protected void done()
      Called once when the task becomes done, by whichever thread made it so: completion, failure or cancellation. Does nothing here.
    • set

      protected void set(V v)
      Completes the future with v, unless it is already done.
    • setException

      protected void setException(Throwable t)
      Fails the future with t, unless it is already done.
    • get

      Description copied from interface: Future
      Waits for the work to finish and returns its result.
      Specified by:
      get in interface Future<V>
      Returns:
      the result
      Throws:
      InterruptedException - when the waiting thread is interrupted
      ExecutionException - when the work threw; Throwable.getCause() is what it threw
    • get

      public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
      Description copied from interface: Future
      Waits up to timeout for the work to finish and returns its result.
      Specified by:
      get in interface Future<V>
      Parameters:
      timeout - how long to wait, in unit
      unit - the unit of timeout
      Returns:
      the result
      Throws:
      InterruptedException - when the waiting thread is interrupted
      ExecutionException - when the work threw
      TimeoutException - when it has not finished in time
    • run

      public void run()
      Description copied from interface: RunnableFuture
      Computes the result and completes this future with it, unless it was cancelled.
      Specified by:
      run in interface Runnable
      Specified by:
      run in interface RunnableFuture<V>
    • runAndReset

      protected boolean runAndReset()
      Runs the task without setting a result, leaving the future ready to run again: for work that repeats. False when the task failed, was cancelled, or was not runnable.
    • toString

      public String toString()
      Description copied from class: Object
      Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode())
      Overrides:
      toString in class Object