User problem: Fetch the order summary of a certain customer.
This time, the asynchronous task must return a result, and so runAsync() is not useful. This is a job for supplyAsync(). It takes Supplier<T> and returns CompletableFuture<T>. T is the type of the result obtained from this supplier via the get() method. In code lines, we can solve this problem as follows:
public static void fetchOrderSummary() {
CompletableFuture<String> cfOrderSummary
= CompletableFuture.supplyAsync(() -> {
logger.info(() -> "Fetch order summary by: "
+ Thread.currentThread().getName());
Thread.sleep(500);
return "Order Summary #93443";
});
// wait for summary to be available, this is blocking
String summary = cfOrderSummary.get();
logger.info(() -> "Order summary: " + summary + " ");
}