CountDownLatch的使用(结合案例)

本文介绍了CountDownLatch的概念和作用,它是一个同步工具类,用于等待多个线程完成任务。通过实例展示了如何在Java中使用线程池和CountDownLatch实现任务同步,确保所有子任务执行完毕后,主线程再继续执行。CountDownLatch通过计数器减少,当计数到0时,所有等待的线程被释放。应用场景包括数据同步等场景。
摘要由CSDN通过智能技术生成

本篇只是一个案例中使用CountDownLatch分享,未涉及深层次的CountDownLatch原理和实现等内容,案例中的工具类是线程池技术+CountDownLatch计数器。

1.CountDownLatch概念

最好的概念就是类中的注释,所以这里我先把源码中的注释搬来然后再进行解释

A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.
A CountDownLatch is initialized with a given count. The await methods block until the current count reaches zero due to invocations of the countDown method, after which all waiting threads are released and any subsequent invocations of await return immediately. This is a one-shot phenomenon -- the count cannot be reset. If you need a version that resets the count, consider using a CyclicBarrier.
A CountDownLatch is a versatile synchronization tool and can be used for a number of purposes. A CountDownLatch initialized with a count of one serves as a simple on/off latch, or gate: all threads invoking await wait at the gate until it is opened by a thread invoking countDown. A CountDownLatch initialized to N can be used to make one thread wait until N threads have completed some action, or some action has been completed N times.

看了后翻一下大致意思就是:

CountDownLatch是一个同步工具类,它允许一个或多个线程一直等待,直到其他线程执行完后再执行。
大白话解释:当我所有要求的任务完成后,我才可以去进行下一步的执行,分别的任务就是线程,CountDownLatch是记录我需要完成多少个任务才会进行下一步!

人话—>好,现在明白了CountDownLatch的作用其实就是一个计数器,用来实时的记录我当前还需要多少任务,然后进行倒计时就完事儿了。

2.CountDownLatch工具类原理

CountDownLatch是通过一个计数器来实现的,初始值为线程的数量。每当一个线程完成了自己的任务后,计数器的值就相应得减1。当计数器到达0时,表示所有的线程都已完成任务,然后在闭锁上等待的线程就可以恢复执行任务。

使用的构造器和关键方法

CountDownLatch countDownLatch = new CountDownLatch(threadNum);  //创建CountDownLatch 对象
countDownLatch.countDown(); //计数器计数减1
countDownLatch.await();  //  使当前线程在锁存器倒计数至零之前一直等待,除非线程被中断

3.应用场景

最近公司的任务对数据的同步有一定的要求,因此用到了此工具类,大致的任务就是希望在一定的任务完成后去返回一个result或者是去发消息队列等最终总结性的操作。

真实场景代码:

@Bean
public Executor learnThreadTaskExecutor() {   // 注入线程执行
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(10);  		//核心线程数
    executor.setMaxPoolSize(10);		//最大线程数
    executor.setQueueCapacity(200);		//队列最大长度
    executor.setKeepAliveSeconds(60);	//线程池维护线程所允许的空闲时间
    executor.setThreadNamePrefix("tsak-asyn");//线程前缀名称
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //线程池对拒绝任务(无线程可用)的处理策略 
    executor.setWaitForTasksToCompleteOnShutdown(true);  //监控任务结束关闭线程池
    return executor;
}


@Resource(name="learnThreadTaskExecutor")
private ThreadPoolTaskExecutor threadPoolTaskExecutor;

int timeSize = xxx;  // 上游数据获取需要的线程数
CountDownLatch countDownLatch=new CountDownLatch(timeSize);
for(int i = 0; i < timeSize; i++){
	//数据内容查询和数据操作(可有可无)
	threadPoolTaskExecutor.submit(new Runnable() {   //线程池
	      @Override
          public void run() {
             try {
                  //数据内容查询和数据操作(查表/缓存,落表/缓存)
             }catch (Exception e) {
                  log.error("操作异常",e);
             }finally{
                countDownLatch.countDown(); //当前线程的任务执行完毕,任务计数器-1
          }
          }
     });
     countDownLatch.await();    //主线程等待所有的子任务结束,如果有一个子任务没有完成则会一直等待
     return result;
}

翻译成人话:只有timeSize个数的任务全部完成,且计数器技术归0,才会执行主线程后的任务。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值