public class T {
public static void main(String[] args) throws InterruptedException {
final int size = 5;
CountDownLatch countDownLatch = new CountDownLatch(size);
for (int i = 0; i < size; i++) {
new Thread(() -> {
System.out.println(Thread.currentThread().getName() + " start");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " end");
countDownLatch.countDown();
}, "Thread" + i).start();
}
System.out.println("main thread await");
countDownLatch.await();
System.out.println("main thread finishes await");
}
}
Thread0 start
Thread2 start
Thread1 start
Thread3 start
Thread4 start
main thread await
Thread1 end
Thread4 end
Thread2 end
Thread3 end
Thread0 end
main thread finishes await