# Countdownlatch

  • 用来进行线程同步协作,等待所有线程完成倒计时。
  • 其中构造参数用来初始化等待计数值,awaitO)用来等待计数归零,countDown0用来让计数减一 类似于倒计时
@Slf4j
public class CountDownLatchTest {
    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(3);

        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                log.info(Thread.currentThread().getName() + "start");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                log.info(Thread.currentThread().getName() + " do something finish");
                countDownLatch.countDown();
            }).start();

        }

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("all thread finish");
    }
}