October 2019
Intermediate to advanced
434 pages
11h 54m
English
CompletableFuture was a new addition in Java 8. It provides a future that can be explicitly completed. We can then respond to the completion of the future using a variety of callbacks to transform and respond to our data in a variety of ways.
Here's a basic example of a CompletableFuture that waits for 5 seconds before emitting a String value:
CompletableFuture.supplyAsync { Thread.sleep(5000) "Future is done"}
We can retrieve this value in a blocking fashion like so:
val returnedValue = future.get()
It's more likely that we'll want to run the code in an asynchronous manner and then respond once the value is returned. The following snippet shows one way in which we could do this, that is, by using the thenAccept() method: ...
Read now
Unlock full access