Sub topic 3
Getting the Result: `join` versus `get`
This chapter assumes Java 8 lambdas, generics, basic threads, and ExecutorService. It also assumes you know that a CompletableFuture<T> represents a result that may become available later.
A CompletableFuture lets you describe asynchronous work without immediately waiting for it. At some point, however, a caller may need the value. The two main methods for retrieving it are join() and get().
Both methods wait if the future is incomplete:
T value = future.join();
T value = future.get();If the task completes normally, both methods return its result. The important difference is how they report failure and interruption.
How `join` behaves
join() waits for completion and returns the result. If the asynchronous task fails, join() throws an unchecked CompletionException.
Unchecked exceptions do not need to appear in a method's throws clause. This makes join() convenient inside lambdas and stream operations.
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
public class JoinDemo {
public static void main(String[] args) {
CompletableFuture<Integer> parsed = CompletableFuture.supplyAsync(
() -> Integer.parseInt("not-a-number"));
try {
int result = parsed.join();
System.out.println(result);
} catch (CompletionException ex) {
System.out.println("Task failed: " + ex.getCause());
}
}
}The CompletionException is a wrapper. Its getCause() usually contains the exception thrown by the asynchronous computation. Here, the cause is a NumberFormatException.
If the future was cancelled, join() throws CancellationException, which is also unchecked.
How `get` behaves
get() also waits and returns the result, but its failure model follows the older Future API. It declares checked exceptions:
InterruptedExceptionmeans the waiting thread was interrupted.ExecutionExceptionmeans the task failed. The original failure is available throughgetCause().CancellationExceptionmeans the future was cancelled.
Because interruption is part of the method contract, the caller must handle it or declare it.
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class GetDemo {
public static void main(String[] args) {
CompletableFuture<Integer> parsed = CompletableFuture.supplyAsync(
() -> Integer.parseInt("not-a-number"));
try {
int result = parsed.get();
System.out.println(result);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
System.out.println("Waiting thread was interrupted");
} catch (ExecutionException ex) {
System.out.println("Task failed: " + ex.getCause());
}
}
}Restoring the interrupt flag with Thread.currentThread().interrupt() is important. It preserves the interruption signal for code higher in the call stack.
Choosing between the methods
Use join() when you want concise code and are already working with CompletableFuture pipelines. Use get() when an API requires Future, or when you need to handle interruption explicitly.
| Method | Failure wrapper | Interruption | Timed form |
|---|---|---|---|
| `join()` | Unchecked `CompletionException` | Not declared separately | No |
| `get()` | Checked `ExecutionException` | Checked `InterruptedException` | `get(timeout, unit)` |
Neither method makes the computation asynchronous. The thread that calls the method is blocked until the result is ready. Prefer dependent stages such as thenApply when you can continue the operation without blocking.
flowchart TD
A[CompletableFuture is incomplete] --> B[join or get called]
B --> C[Calling thread waits]
C --> D{How did the task finish?}
D -->|Success| E[Return value]
D -->|Failure| F[join: CompletionException]
D -->|Failure| G[get: ExecutionException]
D -->|Interrupted while waiting| H[get: InterruptedException]Waiting only for a limited time
The timed form of get() stops waiting after a limit:
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class TimedGet {
public static void main(String[] args) {
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
return "ready";
});
try {
System.out.println(future.get(100, TimeUnit.MILLISECONDS));
} catch (TimeoutException ex) {
System.out.println("The result was not ready in time");
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
} catch (Exception ex) {
System.out.println("Task failed: " + ex.getCause());
}
}
}A timeout affects the waiting call. It does not automatically stop the underlying computation.
Exercise
What is the main difference between join() and get() when a CompletableFuture fails?
Show answer
join() throws an unchecked CompletionException, while get() throws a checked ExecutionException and can also require handling InterruptedException.