Class FutureTask<V>
java.lang.Object
java.util.concurrent.FutureTask<V>
- All Implemented Interfaces:
Runnable, Future<V>, 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 -
Method Summary
Modifier and TypeMethodDescriptionbooleancancel(boolean mayInterruptIfRunning) Stops the work if it has not started.protected voiddone()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.Waits up totimeoutfor the work to finish and returns its result.booleanWhetherFuture.cancel(boolean)cancelled the work before it ran.booleanisDone()Whether the work has finished: completed, failed or cancelled.voidrun()Computes the result and completes this future with it, unless it was cancelled.protected booleanRuns the task without setting a result, leaving the future ready to run again: for work that repeats.protected voidCompletes the future withv, unless it is already done.protected voidFails the future witht, unless it is already done.toString()Returns a string representation of the object.
-
Constructor Details
-
FutureTask
-
FutureTask
-
-
Method Details
-
cancel
public boolean cancel(boolean mayInterruptIfRunning) Description copied from interface:FutureStops the work if it has not started. Answers whether it was cancelled; whether work already running is interrupted is up to the implementation and tomayInterruptIfRunning. -
isCancelled
public boolean isCancelled()Description copied from interface:FutureWhetherFuture.cancel(boolean)cancelled the work before it ran.- Specified by:
isCancelledin interfaceFuture<V>- Returns:
- true when cancelled
-
isDone
-
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
Completes the future withv, unless it is already done. -
setException
Fails the future witht, unless it is already done. -
get
Description copied from interface:FutureWaits for the work to finish and returns its result.- Specified by:
getin interfaceFuture<V>- Returns:
- the result
- Throws:
InterruptedException- when the waiting thread is interruptedExecutionException- 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:FutureWaits up totimeoutfor the work to finish and returns its result.- Specified by:
getin interfaceFuture<V>- Parameters:
timeout- how long to wait, inunitunit- the unit oftimeout- Returns:
- the result
- Throws:
InterruptedException- when the waiting thread is interruptedExecutionException- when the work threwTimeoutException- when it has not finished in time
-
run
public void run()Description copied from interface:RunnableFutureComputes the result and completes this future with it, unless it was cancelled.- Specified by:
runin interfaceRunnable- Specified by:
runin interfaceRunnableFuture<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
Description copied from class:ObjectReturns 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())
-