O'Reilly logo

Mastering Concurrency Programming with Java 9 - 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

The main() method

We have organized the execution of the tasks in the main() method. First, we execute the LoadTask using the supplyAsync() method of the CompletableFuture class. We are going to wait three seconds before the start of the LoadTask to show how the delayExecutor() method works.

public class CompletableMain { 
 
  public static void main(String[] args) { 
    Path file = Paths.get("data","category"); 
    System.out.println(new Date() + ": Main: Loading products 
                       after three seconds...."); 
    LoadTask loadTask = new LoadTask(file); 
 
    CompletableFuture<List<Product>>loadFuture = CompletableFuture 
                       .supplyAsync(loadTask,CompletableFuture 
                       .delayedExecutor(3, TimeUnit.SECONDS)); 

Then, with the resultant CompletableFuture, we use thenApplyAsync() to execute ...

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