JUC-java并发编程-并发工具类-CountDownLatch

7 篇文章 0 订阅
6 篇文章 0 订阅
本文详细介绍了CountDownLatch在Java中的使用,通过两个示例展示了如何在多线程中进行协作,确保主线程在所有子线程完成后启动。第一个例子是单机场景下的线程同步,第二个是模拟游戏加载,等待所有资源加载完毕。
摘要由CSDN通过智能技术生成

JUC

CountDownLatch

countDownLatch

用来进行线程同步协作,等待所有线程完成倒计时。

例子:

public class CountDownLatchTest2 {

    public static void main(String[] args) {

        CountDownLatch countDownLatch = new CountDownLatch(3);

        new Thread(()->{
            log.info("begin...");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //线程调用countDown()方法,让计数减一
            countDownLatch.countDown();
            log.info("end...");
        }).start();

        new Thread(()->{
            log.info("begin...");
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            countDownLatch.countDown();
            log.info("end...");
        }).start();

        new Thread(()->{
            log.info("begin...");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            countDownLatch.countDown();
            log.info("end...");
        }).start();

        log.info("waiting...");
        try {
            //主线程调用await()进入阻塞
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("end...");

    }

}

countDownLatch的应用

场景在单机模式下,主线程等待其他线程结束,最后主线程恢复运行

public class CountDownLatchTest {

    public static void main(String[] args) {
        //模拟游戏加载,等待所有线程都结束,游戏开始
        ExecutorService service = Executors.newFixedThreadPool(10);
        CountDownLatch countDownLatch = new CountDownLatch(10);
        String[] array = new String[10];
        Random random = new Random();
        for(int j =0;j<10;j++){
            int k = j;
            service.submit(()->{
                for(int i=0;i<=100;i++){
                    try {
                        Thread.sleep(random.nextInt(100));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    array[k] = i+"%";
                    //小技巧通过 System.out.print + "\r" 覆盖上一次打印
                    System.out.print("\r"+Arrays.toString(array));
                }
                countDownLatch.countDown();
            });
        }
        try {
            countDownLatch.await();
            System.out.println("");
            log.info("游戏开始");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值