Creating and Completing CompletableFutures
Use supplyAsync, runAsync, completedFuture, and manually complete futures with complete and completeExceptionally.
What Is CompletableFuture?
CompletableFuture<T> (Java 8+) represents an asynchronous computation that can be explicitly completed, chained, and combined. It implements both Future and CompletionStage.
supplyAsync: Starting an Async Computation
CompletableFuture.supplyAsync() runs a Supplier on the common ForkJoinPool and returns a future of the result.
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
// runs on ForkJoinPool.commonPool()
return fetchDataFromNetwork();
});
System.out.println("Non-blocking — this runs immediately");All lessons in this course
- Creating and Completing CompletableFutures
- Chaining with thenApply and thenCompose
- Combining Futures: allOf and anyOf
- Error Handling and Async HTTP Pipeline