Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Future.get: retrieve a task result or handle its failure

Retrieve an asynchronous task’s result and distinguish a task failure from interruption, cancellation, a wait timeout, or a failure of the `invokeAll` call itself.

Version: Java SE 21

Syntax

java.util.concurrent.Future<V>
V get() throws InterruptedException, ExecutionException
V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException

Future.get retrieves the result of an asynchronous computation represented by a java.util.concurrent.Future<V>. The no-argument get() blocks the calling thread if necessary, with no timeout. On success it returns a value of type V, which can be null if that was the task’s result.

If the task throws an exception, get() throws ExecutionException. Use getCause() to inspect the original exception. By contrast, InterruptedException means the calling thread was interrupted while waiting, not that the task failed. If the task was cancelled, get() throws the unchecked CancellationException.

The overload get(long timeout, TimeUnit unit) has the same outcomes but limits how long the caller waits. If the wait times out, it throws TimeoutException. A timeout by itself does not cancel the task; you can call get() again later.

When ExecutorService.invokeAll(tasks) returns normally, its futures are already done and are listed in the collection’s task order. isDone() does not mean success: a task’s exception is reported as ExecutionException by get() on its future, not by invokeAll itself. The invokeAll call can itself be interrupted while waiting (InterruptedException) or be unable to schedule a task (RejectedExecutionException). The timed invokeAll overload cancels unfinished tasks before returning; get() on a cancelled future throws CancellationException.

Examples

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class FutureGetExample {
    public static void main(String[] args) throws InterruptedException {
        List<Callable<Integer>> tasks = List.of(
                () -> 21 * 2,
                () -> { throw new IllegalStateException("invalid input"); }
        );

        try (ExecutorService executor = Executors.newSingleThreadExecutor()) {
            List<Future<Integer>> futures = executor.invokeAll(tasks);
            System.out.println("invokeAll returned");
            for (Future<Integer> future : futures) {
                try {
                    System.out.println("Result: " + future.get());
                } catch (ExecutionException e) {
                    Throwable cause = e.getCause();
                    System.out.println("Task failed: " + cause.getClass().getSimpleName()
                            + ": " + cause.getMessage());
                }
            }
        }
    }
}

Result

invokeAll returned
Result: 42
Task failed: IllegalStateException: invalid input

The first line shows that `invokeAll` returned normally. The first `get()` returns `42`; the second throws `ExecutionException`. The program prints the cause’s type and message, not the wrapper exception’s. Try-with-resources closes the executor.

Related lessons

  • Java · Lesson 50 · Intermediate · Version: 2

Sources