How it works...

We can use a CompletableFuture object with two main purposes:

  • Wait for a value or an event that will be produced in future (creating an object and using the complete() and get() or join() methods).
  • To organize a set of tasks to be executed in a determined order so one or more tasks won't start their execution until others have finished their execution.

In this example, we made both uses of the CompletableFuture class. First, we created an instance of this class and sent it as a parameter to a SeedGenerator task. This task uses the complete() method to send the calculated value, and the main() method uses the get() method to obtain the value. The get() method sleeps the current thread until CompletableFuture has been completed. ...

Get Java 9 Concurrency Cookbook - Second Edition 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.