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 to do it...

Follow these steps to implement the example:

  1. First, we're going to implement the auxiliary tasks we will use in the example. Create a class named SeedGenerator  that implements the Runnable interface. It will have a CompletableFuture object as an attribute, and it will be initialized in the constructor of the class:
        public class SeedGenerator implements Runnable {            private CompletableFuture<Integer> resultCommunicator;                       public SeedGenerator (CompletableFuture<Integer> completable) {             this.resultCommunicator=completable;           }
  1. Then, implement the run() method. It will sleep the current thread for 5 seconds (to simulate a long operation), calculate a random number between 1 and 10, and then use the complete() method of the ...

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