CountDownLatch的用法

1. CountDownLatch定义

一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。
2.主要方法
public CountDownLatch(int count);
public void countDown();
public void await() throws InterruptedException

构造方法参数指定了计数值(count)实际上就是闭锁需要等待的线程数量
countDown方法,当前线程调用此方法,则计数减一
await方法,调用此方法会一直阻塞当前线程,直到计时器的值为0

3.例子

public class CountDownLatchDemo {
    final static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch=new CountDownLatch(2);//两个工人的协作
        Worker worker1=new Worker("zhang san", 5000, latch);
        Worker worker2=new Worker("li si", 8000, latch);
        worker1.start();//
        worker2.start();//
        latch.await();//等待所有工人完成工作
        System.out.println("all work done at "+sdf.format(new Date()));
    }


    static class Worker extends Thread{
        String workerName; 
        int workTime;
        CountDownLatch latch;
        public Worker(String workerName ,int workTime ,CountDownLatch latch){
             this.workerName=workerName;
             this.workTime=workTime;
             this.latch=latch;
        }
        public void run(){
            System.out.println("Worker "+workerName+" do work begin at "+sdf.format(new Date()));
            doWork();//工作了
            System.out.println("Worker "+workerName+" do work complete at "+sdf.format(new Date()));
            latch.countDown();//工人完成工作,计数器减一

        }

        private void doWork(){
            try {
                Thread.sleep(workTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


}

输出:
Worker zhang san do work begin at 2011-04-14 11:05:11
Worker li si do work begin at 2011-04-14 11:05:11
Worker zhang san do work complete at 2011-04-14 11:05:16
Worker li si do work complete at 2011-04-14 11:05:19
all work done at 2011-04-14 11:05:19

原文链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CountDownLatch是Java中提供的一个同步工具类,它可以用来控制一个或多个线程等待多个线程的操作完成。 CountDownLatch用法如下: 1. 创建一个CountDownLatch对象,指定需要等待的线程数。 ``` CountDownLatch latch = new CountDownLatch(5); ``` 2. 每个需要等待的线程执行完后调用CountDownLatch的countDown方法减少计数器。 ``` latch.countDown(); ``` 3. 等待所有线程执行完毕后,调用CountDownLatch的await方法进行等待。 ``` latch.await(); ``` 4. 在所有线程执行完毕后,主线程或其他线程可以继续执行自己的操作。 示例代码: ``` import java.util.concurrent.CountDownLatch; public class CountDownLatchDemo { public static void main(String[] args) throws InterruptedException { CountDownLatch latch = new CountDownLatch(5); for (int i = 0; i < 5; i++) { new Thread(() -> { System.out.println(Thread.currentThread().getName() + " is working..."); try { // 模拟线程执行时间 Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " done"); latch.countDown(); }).start(); } System.out.println("Waiting for all threads to finish..."); latch.await(); System.out.println("All threads have finished, continue to execute the main thread."); } } ``` 运行结果: ``` Waiting for all threads to finish... Thread-0 is working... Thread-2 is working... Thread-1 is working... Thread-3 is working... Thread-4 is working... Thread-1 done Thread-3 done Thread-0 done Thread-2 done Thread-4 done All threads have finished, continue to execute the main thread. ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值