// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import java.util.concurrent.*;
class Main {
public static void main(String[] args) {
CountDownLatch count = new CountDownLatch(3);
new Thread(()->{
System.out.println("Hi there...1");
try{
Thread.sleep(1000);
count.countDown();
} catch(InterruptedException e){
}
System.out.println("Complete...1");
}).start();
new Thread(()->{
System.out.println("Hi there...2");
try{
Thread.sleep(1000);
count.countDown();
} catch(InterruptedException e){
}
System.out.println("Complete...2");
}).start();
new Thread(()->{
System.out.println("Hi there...3");
try{
Thread.sleep(1000);
count.await();
} catch(InterruptedException e){
}
System.out.println("Complete...3");
}).start();
new Thread(()->{
System.out.println("Hi there...4");
try{
Thread.sleep(1000);
count.countDown();
} catch(InterruptedException e){}
System.out.println("Complete...4");
}).start();
System.out.println("........Using Completable future...");
CompletableFuture<Void> task1 = CompletableFuture.runAsync(
()-> {
System.out.println("task1 CompletableFuture started");
try{
Thread.sleep(1000);
count.countDown();
} catch(InterruptedException e){}
System.out.println("CompletableFuture Complete...1");
}
);
CompletableFuture<Void> task2 = CompletableFuture.runAsync(
()-> {
System.out.println("task2 CompletableFuture started");
try{
Thread.sleep(1000);
count.countDown();
} catch(InterruptedException e){}
System.out.println("CompletableFuture Complete...2");
}
);
CompletableFuture<Void> task4 = CompletableFuture.runAsync(
()-> {
System.out.println("task4 CompletableFuture started");
try{
Thread.sleep(1000);
count.countDown();
} catch(InterruptedException e){}
System.out.println("CompletableFuture Complete...4");
});
CompletableFuture<Void> task3 = CompletableFuture.allOf(task1, task2, task4).thenRun(
()-> {
System.out.println("task3 CompletableFuture started");
try{
Thread.sleep(1000);
count.countDown();
} catch(InterruptedException e){}
System.out.println("CompletableFuture Complete...3");
}
);
}
}