September 2019
Intermediate to advanced
816 pages
18h 47m
English
Let's assume that we want to download the following list of invoices:
List<String> invoices = Arrays.asList("#2334", "#122", "#55");
This can be seen as a bunch of independent tasks that can be accomplished in parallel, so we can do it using CompletableFuture as follows:
public static CompletableFuture<String> downloadInvoices(String invoice) { return CompletableFuture.supplyAsync(() -> { logger.info(() -> "Downloading invoice: " + invoice); return "Downloaded invoice: " + invoice; });}CompletableFuture<String> [] cfInvoices = invoices.stream() .map(CustomerAsyncs::downloadInvoices) .toArray(CompletableFuture[]::new);
At this point, we have an array of CompletableFuture instances, and, therefore, an array of asynchronous ...