Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.
public class FutureTask<V>
- Object
- FutureTask
ImplementsFuture<V>, Runnable, 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.
Constructors
public FutureTask(Callable<V> callable) | |
public FutureTask(Runnable runnable, V result) |
Methods
public boolean cancel(boolean mayInterruptIfRunning) | Stops the work if it has not started. |
public boolean isCancelled() | Whether cancel cancelled the work before it ran. |
public boolean isDone() | Whether the work has finished: completed, failed or cancelled. |
protected void done() | Called once when the task becomes done, by whichever thread made it so: completion, failure or cancellation. |
protected void set(V v) | Completes the future with v, unless it is already done. |
protected void setException(Throwable t) | Fails the future with t, unless it is already done. |
public V get()
throws InterruptedException, ExecutionException | Waits for the work to finish and returns its result. |
public V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException | Waits up to timeout for the work to finish and returns its result. |
public void run() | Computes the result and completes this future with it, unless it was cancelled. |
protected boolean runAndReset() | Runs the task without setting a result, leaving the future ready to run again: for work that repeats. |
public String toString() | Returns a string representation of the object. |
Inherited methods
Constructor details
FutureTask
public FutureTask(Callable<V> callable)FutureTask
public FutureTask(Runnable runnable, V result)Method details
cancel
public boolean cancel(boolean mayInterruptIfRunning)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.Parameters
mayInterruptIfRunningboolean- whether a running task may be interrupted
Returns
true when this call cancelled it
isCancelled
public boolean isCancelled()Whether
cancel cancelled the work before it ran.Returns
true when cancelled
isDone
public boolean isDone()Whether the work has finished: completed, failed or cancelled.
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
public V get()
throws InterruptedException, ExecutionExceptionWaits for the work to finish and returns its result.
Returns
the result
Throws
InterruptedException- when the waiting thread is interrupted
ExecutionException- when the work threw;
Throwable.getCauseis what it threw CancellationException- when the work was cancelled
get
public V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutExceptionWaits up to
timeout for the work to finish and returns its result.Parameters
timeoutlong- how long to wait, in
unit unitTimeUnit- 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
CancellationException- when the work was cancelled
run
public void run()Computes the result and completes this future with it, unless it was
cancelled.
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()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())