import java.util.concurrent.CountDownLatch;
public class CountDownLatchExample {
public static void main(String[] args) throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(3);
Thread thread1 = new Thread(() -> {
try {
Thread.sleep(1000);
System.out.println("Thread 1 completed");
latch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread thread2 = new Thread(() -> {
try {
Thread.sleep(2000);
System.out.println("Thread 2 completed");
latch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread thread3 = new Thread(() -> {
try {
Thread.sleep(3000);
System.out.println("Thread 3 completed");
latch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
thread1.start();
thread2.start();
thread3.start();
latch.await();
System.out.println("All threads completed");
}
}