JAVA多线程进阶篇 4、JUC同步器之Countdownlatch


JUC 即 java.util.concurrent 包,提供了大量的工具类来简化并发编程。

Countdownlatch 门闩(shuan),是一种线程同步器,基于AQS,用于多个线程集结。

1 用法示例一

线程都去处理任务,当任务都做完的时候通知主线程。

public class CountDownLatchTest {

    public static void main(String[] args) throws InterruptedException {

        CountDownLatch latch1 = new CountDownLatch(10);

        Thread[] threads = new Thread[10];
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(() -> {
                System.out.println(Thread.currentThread().getName()+":处理任务1");
                latch1.countDown();
            });
        }

        for (Thread t : threads) {
            t.start();
        }

        latch1.await();
        System.out.println("所有任务 都被处理完毕");
    }
}

countdownlatch0

2. Thread.Join方法也可以达到类似效果

线程的join方法也能够实现这样的效果。

public class JoinTest {

    public static void main(String[] args) throws InterruptedException {

        Thread[] threads = new Thread[10];
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + ":处理任务1");
            });
        }

        for (Thread t : threads) {
            t.start();
        }

        //全部集结后再出发
        for (Thread t : threads) {
            t.join();
        }

        System.out.println("所有任务 都被处理完毕");
    }
}

countdownlatch0

3. Countdownlatch具有更强大的功能,示例二

假设线程需要做两个任务,在任务1都完成时,放下门闩1,在任务2时,放下门闩2

public class CountDownLatchTest0 {
    public static void main(String[] args) throws InterruptedException {

        CountDownLatch latch1 = new CountDownLatch(10);
        CountDownLatch latch2 = new CountDownLatch(10);

        Thread[] threads = new Thread[10];
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(() -> {
                System.out.println("处理任务1");
                latch1.countDown();
                //后续任务耗时比较久
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("处理任务2");
                latch2.countDown();
            });
        }

        for (Thread t : threads) {
            t.start();
        }

        latch1.await();
        System.out.println("所有任务1 都被处理完毕");
        latch2.await();
        System.out.println("所有任务 都被处理完毕");
    }
}

countdownlatch

总结

Countdownlatch提供了更灵活的线程同步方法,能够在线程完成后进行集结,且可以设置多个状态标志,使得程序更灵活。

多线程系列在github上有一个开源项目,主要是本系列博客的实验代码。

https://github.com/forestnlp/concurrentlab

如果您对软件开发、机器学习、深度学习有兴趣请关注本博客,将持续推出Java、软件架构、深度学习相关专栏。

您的支持是对我最大的鼓励。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悟空学编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值