Using the CompletableFuture class

Using the CompletableFuture object, we can separate sending the request for data to the measuring system (and creating the CompletableFuture object) from getting the result from the CompletableFuture object. This is exactly the scenario we described when explaining what asynchronous processing is. Let's demonstrate it in the code. Similar to the way we submitted the requests to a measuring system, we can do it using the CompletableFuture.supplyAsync() static method:

List<CompletableFuture<Double>> list = ids.stream()        .map(id -> CompletableFuture.supplyAsync(() -> mSys.apply(id)))        .collect(Collectors.toList());

The difference is that the supplyAsync() method does not wait for the call to the measuring system ...

Get Introduction to Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.