Java Reference ยท Concurrency
The Future interface
Reference for java.util.concurrent.Future and FutureTask: get with timeouts, cancel, isDone, ExecutionException wrapping, and the Java 19 state() methods.
A Future<V> is a receipt for a computation that may still be running. You get one when you submit a task to an ExecutorService. Later you can ask whether the task is done, wait for its result with get(), or try to cancel it.
Future is deliberately small: it cannot be completed by hand, chained or combined. For pipelines of asynchronous steps use CompletableFuture, which also implements Future.
Future<Integer> f = pool.submit(() -> compute()); // ... do other work ... int result = f.get(); // blocks until done
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public interface Future<V>
| Method | Description |
|---|---|
V get() |
Waits until the task completes, then returns the result. |
V get(long timeout, TimeUnit unit) |
Waits at most the given time. |
boolean cancel(boolean mayInterruptIfRunning) |
Tries to cancel. Returns false if the task already completed or was cancelled. |
boolean isDone() |
true after normal completion, an exception or cancellation. |
boolean isCancelled() |
true if cancelled before completing normally. |
V resultNow() |
Java 19+. Returns the result of a successfully completed task without waiting; throws IllegalStateException otherwise. |
Throwable exceptionNow() |
Java 19+. Returns the failure of a task that failed; throws IllegalStateException otherwise. |
Future.State state() |
Java 19+. One of RUNNING, SUCCESS, FAILED, CANCELLED. |
Throws from get(): ExecutionException (the task threw; read getCause()), CancellationException, InterruptedException (the waiting thread was interrupted) and, for the timed form, TimeoutException.
Usage notes
get()blocks. Call it as late as possible, ideally after you have started all the tasks, or the work runs one task at a time.- Failures arrive wrapped. A task's exception is always the cause of an
ExecutionException, never thrown directly. - A timeout does not stop the task.
get(timeout)just stops waiting; callcancel(true)if you also want to interrupt it. - Cancelling is cooperative.
cancel(true)interrupts the thread, so the task must respond to interrupts. A task that has not started never runs; a running one that ignores interrupts keeps going, althoughisCancelled()is alreadytrue. - Submit
Runnableand you getnull.Future<?>fromsubmit(Runnable)is only useful to wait for completion or catch failures. FutureTaskis the standard implementation. It is both aRunnableand aFuture, so you can run it on a plainThreador an executor.- Not composable. Chaining, combining and callbacks need
CompletableFuture.