# FutureTask

public class **FutureTask** extends [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)  
implements [RunnableFuture](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/RunnableFuture.html)\<V\> A cancellable asynchronous computation. This class provides a base
implementation of `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html`, 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`
methods will block if the computation has not yet completed. Once
the computation has completed, the computation cannot be restarted
or cancelled (unless the computation is invoked using
`https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/FutureTask.html#runAndReset()`).

A `FutureTask` can be used to wrap a `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Callable.html` or
`https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Runnable.html` object. Because `FutureTask` implements
`Runnable`, a `FutureTask` can be submitted to an
`https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Executor.html` for execution.

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

### Public Constructor Summary

|---|---|
|   | [FutureTask](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/FutureTask.html#FutureTask(java.util.concurrent.Callable<V>))([Callable](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Callable.html)\<V\> callable) Creates a `FutureTask` that will, upon running, execute the given `Callable`. |
|   | [FutureTask](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/FutureTask.html#FutureTask(java.lang.Runnable,%20V))([Runnable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Runnable.html) 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. |

### Public Method Summary

|---|---|
| boolean | [cancel](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/FutureTask.html#cancel(boolean))(boolean mayInterruptIfRunning) Attempts to cancel execution of this task. |
| V | [get](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/FutureTask.html#get(long,%20java.util.concurrent.TimeUnit))(long timeout, [TimeUnit](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/TimeUnit.html) unit) Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available. |
| V | [get](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/FutureTask.html#get())() Waits if necessary for the computation to complete, and then retrieves its result. |
| boolean | [isCancelled](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/FutureTask.html#isCancelled())() Returns `true` if this task was cancelled before it completed normally. |
| boolean | [isDone](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/FutureTask.html#isDone())() Returns `true` if this task completed. |
| void | [run](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/FutureTask.html#run())() Sets this Future to the result of its computation unless it has been cancelled. |

### Protected Method Summary

|---|---|
| void | [done](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/FutureTask.html#done())() Protected method invoked when this task transitions to state `isDone` (whether normally or via cancellation). |
| boolean | [runAndReset](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/FutureTask.html#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. |
| void | [set](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/FutureTask.html#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. |
| void | [setException](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/FutureTask.html#setException(java.lang.Throwable))([Throwable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Throwable.html) t) Causes this future to report an `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ExecutionException.html` with the given throwable as its cause, unless this future has already been set or has been cancelled. |

### Inherited Method Summary

From class [java.lang.Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)

|---|---|
| [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [clone](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#clone())() Creates and returns a copy of this `Object`. |
| boolean | [equals](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#equals(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) obj) Compares this instance with the specified object and indicates if they are equal. |
| void | [finalize](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#finalize())() Invoked when the garbage collector has detected that this instance is no longer reachable. |
| final [Class](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html)\<?\> | [getClass](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#getClass())() Returns the unique instance of `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html` that represents this object's class. |
| int | [hashCode](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#hashCode())() Returns an integer hash code for this object. |
| final void | [notify](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notify())() Causes a thread which is waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| final void | [notifyAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notifyAll())() Causes all threads which are waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) | [toString](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#toString())() Returns a string containing a concise, human-readable description of this object. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long,%20int))(long timeout, int nanos) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long))(long timeout) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait())() Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object. |

From interface [java.util.concurrent.RunnableFuture](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/RunnableFuture.html)

|---|---|
| abstract void | [run](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/RunnableFuture.html#run())() Sets this Future to the result of its computation unless it has been cancelled. |

From interface [java.lang.Runnable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Runnable.html)

|---|---|
| abstract void | [run](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Runnable.html#run())() When an object implementing interface `Runnable` is used to create a thread, starting the thread causes the object's `run` method to be called in that separately executing thread. |

From interface [java.util.concurrent.Future](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html)

|---|---|
| abstract boolean | [cancel](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html#cancel(boolean))(boolean mayInterruptIfRunning) Attempts to cancel execution of this task. |
| abstract V | [get](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html#get(long,%20java.util.concurrent.TimeUnit))(long timeout, [TimeUnit](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/TimeUnit.html) unit) Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available. |
| abstract V | [get](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html#get())() Waits if necessary for the computation to complete, and then retrieves its result. |
| abstract boolean | [isCancelled](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html#isCancelled())() Returns `true` if this task was cancelled before it completed normally. |
| abstract boolean | [isDone](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html#isDone())() Returns `true` if this task completed. |

## Public Constructors

#### public
**FutureTask**
([Callable](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Callable.html)\<V\> callable)

Creates a `FutureTask` that will, upon running, execute the
given `Callable`.

##### Parameters

| callable | the callable task |
|---|---|

##### Throws

| [NullPointerException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | if the callable is null |
|---|---|

#### public
**FutureTask**
([Runnable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Runnable.html) 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<Void>(runnable, null)` |
|---|---|

##### Throws

| [NullPointerException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | if the runnable is null |
|---|---|

## Public Methods

#### public boolean
**cancel**
(boolean mayInterruptIfRunning)

Attempts to cancel execution of this task. This attempt will
fail if the task has already completed, has 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.

After this method returns, subsequent calls to `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html#isDone()` will
always return `true`. Subsequent calls to `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html#isCancelled()`
will always return `true` if this method returned `true`.

##### 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

#### public V
**get**
(long timeout, [TimeUnit](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/TimeUnit.html) unit)

Waits if necessary for at most the given time for the computation
to complete, and then retrieves its result, if available.

##### Parameters

| timeout | the maximum time to wait |
| unit | the time unit of the timeout argument |
|---|---|

##### Returns

- the computed result

##### Throws

| [CancellationException](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CancellationException.html) |   |
| [InterruptedException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/InterruptedException.html) |   |
| [ExecutionException](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ExecutionException.html) |   |
| [TimeoutException](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/TimeoutException.html) |   |
|---|---|

#### public V
**get**
()

Waits if necessary for the computation to complete, and then
retrieves its result.

##### Returns

- the computed result

##### Throws

| [CancellationException](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CancellationException.html) |   |
| [InterruptedException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/InterruptedException.html) |   |
| [ExecutionException](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ExecutionException.html) |   |
|---|---|

#### public boolean
**isCancelled**
()

Returns `true` if this task was cancelled before it completed
normally.

##### Returns

- `true` if this task was cancelled before it completed

#### public boolean
**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`.

##### Returns

- `true` if this task completed

#### public void
**run**
()

Sets this Future to the result of its computation
unless it has been cancelled.

## Protected Methods

#### 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.

#### 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

#### 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.

This method is invoked internally by the `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/FutureTask.html#run()` method
upon successful completion of the computation.

##### Parameters

| v | the value |
|---|---|

#### protected void
**setException**
([Throwable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Throwable.html) t)

Causes this future to report an `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ExecutionException.html`
with the given throwable as its cause, unless this future has
already been set or has been cancelled.

This method is invoked internally by the `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/FutureTask.html#run()` method
upon failure of the computation.

##### Parameters

| t | the cause of failure |
|---|---|