Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Java CompletableFuture.allOf: completion barrier, not typed results

Last updated: 30 Sept 20264 min read
tutorial
IntermediateBy AITrove Editorial

CompletableFuture.allOf returns a Void stage that completes after its input stages, and propagates exceptional completion if an input fails.

Keep the original stages

One receipt lookup succeeds and another fails. Joining allOf raises CompletionException; the healthy stage still holds its value. The returned barrier does not collect both results into a typed list. Keep references to the input stages if results or per-stage diagnostics matter.

An exceptional barrier is not a cancellation policy. The fixture uses already-completed stages and cannot show whether other running work stops. CompletableFuture basics covers completion and executor ownership.

Choose a failure policy

Batch requests often need either fail-fast reporting or partial results. Make that choice explicitly. Do not catch the barrier exception and report the whole batch as processed without checking each input's outcome.

Working program

Java
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
public class ReceiptBatchBarrier {
    public static void main(String[] args) {
        CompletableFuture<String> accepted = CompletableFuture.completedFuture("R-41");
        CompletableFuture<String> rejected = new CompletableFuture<>();
        rejected.completeExceptionally(new IllegalStateException("R-42 missing"));
        try { CompletableFuture.allOf(accepted, rejected).join(); }
        catch (CompletionException failure) {
            System.out.println("cause=" + failure.getCause().getClass().getSimpleName());
        }
        System.out.println("accepted=" + accepted.join());
        System.out.println("rejected=" + rejected.isCompletedExceptionally());
    }
}

Output

Output
cause=IllegalStateException
accepted=R-41
rejected=true

Costs and boundaries

The barrier tracks the provided stage completions; it is not a result collection or a throughput measurement. The fixture makes no threads and does not test cancellation of live tasks.

Common Mistakes

  • Do not expect allOf.join to return a list of values.
  • Do not infer that a failed barrier cancelled successful or still-running stages.
  • Do not mask partial failure by swallowing CompletionException.

Read next

Java CompletableFuture: composition, failures and executor ownership, Java cancellation: timed waits and cooperative interruption, Java ExecutorService: bounded admission and shutdown.

java
completablefuture-allof-failure
Storage details