小朋友过生日: 要点燃N根蜡烛(N个线程), 才能唱生日歌(末尾的任务);

  • (1). 然而每个蜡烛都要点, 都是一个线程; countDown 减1
  • (2). N个蜡烛点燃后; 才能唱生日歌:main线程; countDown 减到0了, 才能破除await, 唱歌吃蛋糕!
import lombok.extern.slf4j.Slf4j;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author weijun.nie
 * @Date 2020/4/20 15:11
 * @Version 1.0
 */
@Slf4j
public class CountDownLatchCelebrateBirthday {
    public static void main(String[] args) throws InterruptedException {
        final int AGE = 10;
        CountDownLatch cdl = new CountDownLatch(AGE);

        /**
         * 给小朋友过生日, 小朋友N岁
         */
        ExecutorService exec = Executors.newCachedThreadPool();
        for (int i = 0; i < AGE; i++) {
            int finalI = i;
            exec.submit(() -> {
                try {
                    Thread.currentThread().setName("[生日蜡烛第" + finalI + "个]");
                    // 让main线程先走起;
                    Thread.sleep(500);
                    String name = Thread.currentThread().getName();
                    log.info("点亮{}, 激动的心, 颤抖的手, 燃亮的火柴, 可爱的蜡烛 ,插向蛋糕~", name);

                    long time = new Random().nextInt(10) * 1000;
                    Thread.sleep(time);
                    log.info("{} 点燃并插到蛋糕上了......耗时{} ms", name, time);
                    cdl.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        exec.shutdown();
        log.info("蛋糕摆放好, 等着插蜡烛呢....");
        cdl.await();
        log.info("所有蜡烛都点亮并插上了..一起唱:祝你生日快乐......");
    }
}

console:

15:41:57.353 [main] INFO CountDownLatchCelebrateBirthday - 蛋糕摆放好, 等着插蜡烛呢....
15:41:57.857 [[生日蜡烛第1个]] INFO CountDownLatchCelebrateBirthday - 点亮[生日蜡烛第1个], 激动的心, 颤抖的手, 燃亮的火柴, 可爱的蜡烛 ,插向蛋糕~
15:41:57.860 [[生日蜡烛第0个]] INFO CountDownLatchCelebrateBirthday - 点亮[生日蜡烛第0个], 激动的心, 颤抖的手, 燃亮的火柴, 可爱的蜡烛 ,插向蛋糕~
15:41:57.861 [[生日蜡烛第9个]] INFO CountDownLatchCelebrateBirthday - 点亮[生日蜡烛第9个], 激动的心, 颤抖的手, 燃亮的火柴, 可爱的蜡烛 ,插向蛋糕~
15:41:57.861 [[生日蜡烛第8个]] INFO CountDownLatchCelebrateBirthday - 点亮[生日蜡烛第8个], 激动的心, 颤抖的手, 燃亮的火柴, 可爱的蜡烛 ,插向蛋糕~
15:41:57.861 [[生日蜡烛第7个]] INFO CountDownLatchCelebrateBirthday - 点亮[生日蜡烛第7个], 激动的心, 颤抖的手, 燃亮的火柴, 可爱的蜡烛 ,插向蛋糕~
15:41:57.861 [[生日蜡烛第6个]] INFO CountDownLatchCelebrateBirthday - 点亮[生日蜡烛第6个], 激动的心, 颤抖的手, 燃亮的火柴, 可爱的蜡烛 ,插向蛋糕~
15:41:57.861 [[生日蜡烛第5个]] INFO CountDownLatchCelebrateBirthday - 点亮[生日蜡烛第5个], 激动的心, 颤抖的手, 燃亮的火柴, 可爱的蜡烛 ,插向蛋糕~
15:41:57.861 [[生日蜡烛第4个]] INFO CountDownLatchCelebrateBirthday - 点亮[生日蜡烛第4个], 激动的心, 颤抖的手, 燃亮的火柴, 可爱的蜡烛 ,插向蛋糕~
15:41:57.861 [[生日蜡烛第3个]] INFO CountDownLatchCelebrateBirthday - 点亮[生日蜡烛第3个], 激动的心, 颤抖的手, 燃亮的火柴, 可爱的蜡烛 ,插向蛋糕~
15:41:57.861 [[生日蜡烛第2个]] INFO CountDownLatchCelebrateBirthday - 点亮[生日蜡烛第2个], 激动的心, 颤抖的手, 燃亮的火柴, 可爱的蜡烛 ,插向蛋糕~
15:41:57.861 [[生日蜡烛第8个]] INFO CountDownLatchCelebrateBirthday - [生日蜡烛第8个] 点燃并插到蛋糕上了......耗时0 ms
15:42:01.862 [[生日蜡烛第5个]] INFO CountDownLatchCelebrateBirthday - [生日蜡烛第5个] 点燃并插到蛋糕上了......耗时4000 ms
15:42:01.862 [[生日蜡烛第9个]] INFO CountDownLatchCelebrateBirthday - [生日蜡烛第9个] 点燃并插到蛋糕上了......耗时4000 ms
15:42:04.862 [[生日蜡烛第4个]] INFO CountDownLatchCelebrateBirthday - [生日蜡烛第4个] 点燃并插到蛋糕上了......耗时7000 ms
15:42:05.861 [[生日蜡烛第3个]] INFO CountDownLatchCelebrateBirthday - [生日蜡烛第3个] 点燃并插到蛋糕上了......耗时8000 ms
15:42:05.861 [[生日蜡烛第0个]] INFO CountDownLatchCelebrateBirthday - [生日蜡烛第0个] 点燃并插到蛋糕上了......耗时8000 ms
15:42:05.861 [[生日蜡烛第1个]] INFO CountDownLatchCelebrateBirthday - [生日蜡烛第1个] 点燃并插到蛋糕上了......耗时8000 ms
15:42:05.861 [[生日蜡烛第2个]] INFO CountDownLatchCelebrateBirthday - [生日蜡烛第2个] 点燃并插到蛋糕上了......耗时8000 ms
15:42:06.861 [[生日蜡烛第7个]] INFO CountDownLatchCelebrateBirthday - [生日蜡烛第7个] 点燃并插到蛋糕上了......耗时9000 ms
15:42:06.861 [[生日蜡烛第6个]] INFO CountDownLatchCelebrateBirthday - [生日蜡烛第6个] 点燃并插到蛋糕上了......耗时9000 ms
15:42:06.861 [main] INFO CountDownLatchCelebrateBirthday - 所有蜡烛都点亮并插上了..一起唱:祝你生日快乐......

丰木
322 声望19 粉丝

遇见超乎想象的自己!