Java8 - Combining and chaining Completable Future instances

Category : Java | Sub Category : Java8 Features | By Prasad Bonam Last updated: 2023-11-13 07:52:12 Viewed : 234


Combining and chaining CompletableFuture instances in Java 8 allows you to create complex asynchronous workflows. You can combine results from multiple asynchronous computations and chain them together to form a sequence of operations. Here are examples of combining and chaining CompletableFuture instances:

1. Combining Results with thenCombine and thenAcceptBoth:

  • thenCombine: Combines the results of two CompletableFuture instances when both are complete.

    java
    CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello"); CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> " World"); CompletableFuture<String> combinedFuture = future1.thenCombine(future2, (s1, s2) -> s1 + s2);
  • thenAcceptBoth: Similar to thenCombine, but instead of returning a result, it accepts a BiConsumer to perform an action with both results.

    java
    CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello"); CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> " World"); CompletableFuture<Void> actionFuture = future1.thenAcceptBoth(future2, (s1, s2) -> System.out.println(s1 + s2));

2. Chaining with thenApply, thenCompose, and thenAccept:

  • thenApply: Applies a function to the result of the current CompletableFuture and returns a new CompletableFuture with the transformed result.

    java
    CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello"); CompletableFuture<String> transformedFuture = future.thenApply(s -> s + " World");
  • thenCompose: Similar to thenApply, but the function returns a CompletableFuture, allowing for chaining of asynchronous computations.

    java
    CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello"); CompletableFuture<String> composedFuture = future.thenCompose(s -> CompletableFuture.supplyAsync(() -> s + " World"));
  • thenAccept: Accepts a Consumer that performs an action with the result of the current CompletableFuture but doesn`t return a value.

    java
    CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello"); CompletableFuture<Void> actionFuture = future.thenAccept(s -> System.out.println("Result: " + s));

3. Combining Multiple Futures with allOf and anyOf:

  • allOf: Waits for all the provided CompletableFuture instances to complete. It returns a CompletableFuture<Void> that completes when all the input futures are complete.

    java
    CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello"); CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> " World"); CompletableFuture<Void> allOfFuture = CompletableFuture.allOf(future1, future2);
  • anyOf: Waits for any of the provided CompletableFuture instances to complete. It returns a CompletableFuture<Object> that completes exceptionally with a CompletionException if any of the input futures complete exceptionally.

    java
    CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello"); CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> " World"); CompletableFuture<Object> anyOfFuture = CompletableFuture.anyOf(future1, future2);

Example:

Here is a comprehensive example demonstrating combining and chaining CompletableFuture instances:

java
import java.util.concurrent.CompletableFuture; public class CompletableFutureCombiningChainingExample { public static void main(String[] args) throws Exception { CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello"); CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> " World"); // Combining results CompletableFuture<String> combinedFuture = future1.thenCombine(future2, (s1, s2) -> s1 + s2); // Chaining with thenApply CompletableFuture<String> transformedFuture = combinedFuture.thenApply(s -> s + "!"); // Chaining with thenCompose CompletableFuture<String> composedFuture = transformedFuture.thenCompose(s -> CompletableFuture.supplyAsync(() -> s.toUpperCase())); // Chaining with thenAccept CompletableFuture<Void> actionFuture = composedFuture.thenAccept(result -> System.out.println("Final Result: " + result)); // Waiting for completion of all futures CompletableFuture.allOf(future1, future2, combinedFuture, transformedFuture, composedFuture, actionFuture).join(); } }

This example demonstrates combining and chaining CompletableFuture instances to create a pipeline of asynchronous operations. The allOf method is used to wait for the completion of all futures.

Search
Related Articles

Leave a Comment: