Describe a scenario where using `CompletableFuture`'s `thenCombine`, `thenAcceptBoth`, and `runAfterBoth` methods would be beneficial. Explain the nuances of choosing between these methods.

Java interview question for Advanced practice.

Answer

Let's imagine a scenario involving an e-commerce application processing an order. We need to perform two independent, asynchronous operations: (1) fetch the customer's shipping information and (2) fetch the product's current inventory status. CompletableFuture<ShippingInfo shippingFuture = ...; CompletableFuture<InventoryStatus inventoryFuture = ...; Here's how we'd choose between the three methods: thenCombine: Use this when you need to use the results of both futures to produce a new result. For example, we want to create a final ShipmentDetails object that contains both the shipping info and inventory status. CompletableFuture<ShipmentDetails detailsFuture = shippingFuture.thenCombine(inventoryFuture, (shipping, inventory) - new ShipmentDetails(shipping, inventory)); The returned detailsFuture can then be used in subsequent steps. thenAcceptBoth: Use this when you need to perform an action using the results of both futures, but you don't need to produce a new result. For example, logging the combined information. CompletableFuture<Void loggingFuture = shippingFuture.thenAcceptBoth(inventoryFuture, (shipping, inventory) - log.info("Preparing shipment for " + shipping.getAddress() + " with stock " + inventory.getCount())); The returned future is a CompletableFuture<Void. runAfterBoth: Use this when you need to trigger an action after both futures complete, but you do not care about their results. For example, sending a generic "processing started" notification. CompletableFuture<Void notificationFuture = shippingFuture.runAfterBoth(inventoryFuture, () - System.out.println("Shipping and inventory checks initiated.")); Exception Handling: If either of the initial futures (shippingFuture or inventoryFuture) completes exceptionally, none of the actions within thenCombine, thenAcceptBoth, or runAfterBoth will execute. The resulting future (e.g., detailsFuture) will also complete with the same exception. To handle this, you should attach an .exceptionally() handler to the final combined future.

Explanation

The CompletableFuture class is not limited to combining just two futures. You can chain multiple thenCombine or similar operations to combine results from any number of asynchronous computations.

Related Questions