Multithreading: CompletableFuture.thenCombine joins two futures

thenCombine runs a function over the results of two independent CompletableFutures once both have completed, without either one having to wait for the other to start. The combining function itself only runs after both async computations are done.

Code
CompletableFuture<Integer> a = CompletableFuture.supplyAsync(() -> 6);
CompletableFuture<Integer> b = CompletableFuture.supplyAsync(() -> 7);
CompletableFuture<Integer> combined = a.thenCombine(b, (x, y) -> x * y);
System.out.println("Combined result: " + combined.get());
Output
Combined result: 42
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms