September 2019
Intermediate to advanced
816 pages
18h 47m
English
User problem: Deliver an order and notify the customer.
Notifying the customer should be accomplished after delivering the order. This is just an SMS of the Dear customer, your order has been delivered today type, so the notification task doesn't need to know anything about the order. These kinds of tasks can be accomplished by thenRun(). This method takes Runnable and returns CompletableFuture<Void>. Let's see it at work:
public static void deliverOrderNotifyCustomer() { CompletableFuture<Void> cfDeliverOrder = CompletableFuture.runAsync(() -> { logger.info(() -> "Order was delivered by: " + Thread.currentThread().getName()); Thread.sleep(500); }); CompletableFuture<Void> ...