JUC同步锁: CountDownLatch 代码Demo 与 thread.join() Demo 对比

CountDownLatch 流程与 thread.join() 方法类似
CountDownLatch  好比门栓
CountDownLatch latch = new CountDownLatch(number);  //  门栓数量
latch.countDown();	//	倒数计算  CountDownLatch(number) 的 number - 1;
latch.getCount();		//	获取剩余门栓数量,当 latch.getCount()=0 时线程会自动唤醒
latch.await();			//	阻塞,等待 latch.getCount()=0 时开门,继续执行后面的操作...

import java.util.concurrent.CountDownLatch;

public class CountDownLatchDemo {

    public static void main(String[] args) {

		//	thread.join() Demo
        usingJoin(100);

        System.err.println("===================================");
		//	CountDownLatch Demo
        usingCountDownLatch(10, 10000);
    }

    /**
     * @param threadNumber 线程数量
     */
    private static void usingJoin(int threadNumber) {
        Thread[] threads = new Thread[threadNumber];

        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(() -> System.out.println(Thread.currentThread().getName() + "线程冲呀..."));
        }

        System.err.println("线程开始运行");
        for (int i = 0; i < threads.length; i++) threads[i].start();

        System.err.println(" `当前的线程` 在主线程内 调用join方法," +
                " `主线程` 会等待 `当前的线程` 运行完之后才会开始执行后续代码");
        for (int i = 0; i < threads.length; i++) join(threads[i]);

        System.err.println("end usingJoin 当前线程为:" + Thread.currentThread().getName());
    }

    //  等待传入的线程执行完后 调用该线程的线程开始执行
    private static void join(Thread thread) {
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    /**
     * 类似门栓
     *
     * @param number       门槛数量要求
     * @param threadNumber 线程数量
     *                     线程数量 < 门槛数量要求 会一直等待
     *                     线程数量 >= 门槛数量要求 执行后面的流程
     */
    private static void usingCountDownLatch(int number, int threadNumber) {
        Thread[] threads = new Thread[threadNumber];
        CountDownLatch latch = new CountDownLatch(number);  //  门栓数量

        System.out.println("栓上门, 满" + number + "个线程后 才能执行后面的操作,否则一直等待...");

        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "线程冲呀...");
                latch.countDown();  //  倒数计算 并不是直接唤醒线程,当 latch.getCount()为0时线程会自动唤醒
            });
        }

        System.out.println("线程开始运行");
        for (int i = 0; i < threads.length; i++) {
            threads[i].start();

            if (latch.getCount() == 0) {
                if (i + 1 == number) {
                    System.err.println("latch.getCount() 为0了,执行继续后面的操作....");
                } else {
                    System.err.println("latch.getCount() 为0了,但是当前循环没有结束...");
                }
            }
        }

        await(latch);   //  等待

        System.err.println("end usingCountDownLatch");
    }

    //  等候 阻塞
    private static void await(CountDownLatch latch) {
        System.err.println("阻塞中...等待 latch.getCount()=0时,线程继续执行...");
        try {
            latch.await();
            System.err.println("开门了... latch.getCount() 为0,开始执行后面的操作...");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值