Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

CompletableFuture.get(): waiting for a result

Use `get()` to retrieve a result or `get(timeout, unit)` to limit the wait. Unlike `join()`, both overloads declare checked exceptions.

Version: Java SE 21

Syntax

public T get() throws InterruptedException, ExecutionException
public T get(long timeout, java.util.concurrent.TimeUnit unit) throws InterruptedException, ExecutionException, java.util.concurrent.TimeoutException

In Java SE 21, CompletableFuture.get is an instance method of java.util.concurrent.CompletableFuture<T>. A call to get() blocks the calling thread only if it needs to wait for completion. If the future is already complete, there is no wait; on successful completion, the method returns the result of type T.

Both overloads throw InterruptedException if the calling thread is interrupted while waiting, or ExecutionException if the computation fails. Use getCause() to inspect the failure. Cancellation of the future itself throws an unchecked CancellationException. You must catch checked exceptions or declare them with throws.

get(long timeout, TimeUnit unit) limits the wait, for example future.get(2, TimeUnit.SECONDS). If the limit expires before completion, the call throws a checked TimeoutException; the timeout itself neither cancels nor completes the future.

join() can also block and likewise throws CancellationException on cancellation, but it declares no checked exceptions: a computation failure is reported as an unchecked CompletionException. To process the result without a blocking read at the call site, attach a continuation such as thenApply.

Examples

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class Main {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        CompletableFuture<Integer> pending = new CompletableFuture<>();
        try {
            pending.get(0, TimeUnit.MILLISECONDS);
            throw new AssertionError("Expected a timeout");
        } catch (TimeoutException e) {
            System.out.println("Timed out");
        }

        pending.complete(42);
        System.out.println("Result: " + pending.get());

        CompletableFuture<Integer> failed =
                CompletableFuture.failedFuture(new IllegalStateException("No result"));
        try {
            failed.get();
        } catch (ExecutionException e) {
            System.out.println("get cause: " + e.getCause().getMessage());
        }
        try {
            failed.join();
        } catch (CompletionException e) {
            System.out.println("join cause: " + e.getCause().getMessage());
        }
    }
}

Result

Timed out
Result: 42
get cause: No result
join cause: No result

The zero timeout produces `TimeoutException` because `pending` is still incomplete. After `complete(42)`, `get()` returns `42` without waiting. Finally, `get()` and `join()` expose the same failure cause through two different wrapper exceptions.

Related lessons

  • Java · Lesson 51 · Intermediate · Version: 2

Sources