CountDownLatch 的使用

一:定义

闭锁, CountDownLatch 这个类能够使一个线程等待其他线程完成工作后再执行。
CountDownLatch 是通过一个计数器来实现的,计数器的初始值为初始任务 的数量。每当完成了一个任务后,计数器的值就会减 1 CountDownLatch.countDown() 方法)。当计数器值到达 0 时,它表示所有的已经完成了任务,然后在闭锁上 CountDownLatch.await() 方法的线程就可以继续 执行任务

二:应用场景

实现最大的并行性:有时我们想同时启动多个线程,实现最大程度的并行性。
例如,我们想测试一个单例类。如果我们创建一个初始计数为 1
CountDownLatch ,并让所有线程都在这个锁上等待,那么我们可以很轻松地完成
测试。我们只需调用 一次 countDown() 方法就可以让所有的等待线程同时恢复执行。
如下图
TW1 和 TW2 线程,在等待线程A,线程B,线程C,线程D 执行完成后,即count = 0时,TW1和TW2 则继续执行任务。

三:代码说明

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "67890 ";
    CountDownLatch latch = new CountDownLatch(6);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new Thread(new Runnable() {
            public void run() {
                SleepTools.ms(1);
                System.out.println(TAG + "1 Thread_"+Thread.currentThread().getId());
                latch.countDown();
                long count = latch.getCount();
                System.out.println(TAG +  "after countDown, count:" + count);
                SleepTools.ms(1);
                System.out.println(TAG +  "2 Thread_"+Thread.currentThread().getId());
                latch.countDown();
                long count2 = latch.getCount();
                System.out.println(TAG +  "2 after countDown, count2:" + count2);
            }
        }).start();

        // 启动1个业务线程
        new Thread(new BusinessThread()).start();

        // 启动4个业务线程
        for(int i=0;i<=3;i++){
            Thread thread = new Thread(new WorkThread());
            thread.start();
        }

        try {
            latch.await(); // 等待 count=0
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(TAG +  "Main thread end........");
    }
    
    /*工作线程*/
    private  class WorkThread implements Runnable{

        public void run() {
            System.out.println(TAG +  "WorkThread_"+Thread.currentThread().getId() +" run start");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(TAG +  "run after sleep, WorkThread_"+Thread.currentThread().getId());
            latch.countDown();
            long count = latch.getCount();
            System.out.println(TAG +  "run after countDown, count:" + count);

        }
    }

    /*业务线程等待latch的计数器为0时,则恢复执行自己的任务*/
    private  class BusinessThread implements Runnable{

        public void run() {
            try {
                latch.await(); // 等待 count=0
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(TAG +  "BusinessThread_"+Thread.currentThread().getId());
        }
    }
}

打印结果

  • 15
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值