1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
   | package cn.idea360.assistant.dev.sync;
  import java.util.concurrent.CompletableFuture;
 
 
 
 
  public class AsyncTest4 {
      
 
 
 
 
 
 
      public static void main(String[] args) throws Exception {                  CompletableFuture<Void> task1 = CompletableFuture.runAsync(() -> {             try {                 Thread.sleep(1000);                 System.out.println("Task 1 finished");             } catch (InterruptedException e) {                 e.printStackTrace();             }         });
          CompletableFuture<Void> task2 = CompletableFuture.runAsync(() -> {             try {                 Thread.sleep(2000);                 System.out.println("Task 2 finished");             } catch (InterruptedException e) {                 e.printStackTrace();             }         });
          CompletableFuture<Void> task3 = CompletableFuture.runAsync(() -> {             try {                 Thread.sleep(1500);                 System.out.println("Task 3 finished");             } catch (InterruptedException e) {                 e.printStackTrace();             }         });
                   CompletableFuture<Void> allOf = CompletableFuture.allOf(task1, task2, task3);
                   allOf.thenRun(() -> {             System.out.println("All tasks completed, running next task...");         });
                   allOf.join(); 
                   System.out.println("All tasks completed successfully!");     } }
   |