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

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