O'Reilly logo

Java 9 Concurrency Cookbook - Second Edition by Javier Fernández González

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required