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

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.