Skip to content
Tutorials Center
My learning

Topics

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.

Java
Future<Integer> f = pool.submit(() -> compute());
// ... do other work ...
int result = f.get();   // blocks until done

Try it

Java get, timeouts and exceptions

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.

Java Cancelling and the state() method

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

Java
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; call cancel(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, although isCancelled() is already true.
  • Submit Runnable and you get null. Future<?> from submit(Runnable) is only useful to wait for completion or catch failures.
  • FutureTask is the standard implementation. It is both a Runnable and a Future, so you can run it on a plain Thread or an executor.
  • Not composable. Chaining, combining and callbacks need CompletableFuture.