0Pricing
Java Academy · Lesson

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

  1. Creating and Completing CompletableFutures
  2. Chaining with thenApply and thenCompose
  3. Combining Futures: allOf and anyOf
  4. Error Handling and Async HTTP Pipeline
← Back to Java Academy