当前位置: 首页 > news >正文

CountDownLatch使用

常用于多线程场景,待多线程都结束后方可继续主线程逻辑处理

CodeConstant 常量类

import java.util.HashMap;
import java.util.Map;public class CodeConstant {public static final Map<String, Map<String, String>> CODE = new HashMap<>();static {//资产1CODE.put(AssetTypeEnum.ZICHAN_1.getCode(),new HashMap<String, String>() {{put("JS", "JS1");put("CHECK", "CHECK1");put("PUBLISH", "PUBLISH1");}});//资产2CODE.put(AssetTypeEnum.ZICHAN_2.getCode(),new HashMap<String, String>() {{put("JS", "JS1");put("CHECK", "CHECK2");put("PUBLISH", "PUBLISH2");}});}
}

CountDownLatchController

import com.example.demo.constant.CodeConstant;
import com.example.demo.service.TestCountDownLatchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;@RestController
@RequestMapping("/countDownLatch")
public class CountDownLatchController {private static final Logger logger = LoggerFactory.getLogger(CountDownLatchController.class);@Autowiredprivate TestCountDownLatchService downLatchService;@PostMapping("/test")public void testCountDownLatch() {logger.info("测试多线程开始...");try {List<String> list = Arrays.asList("zichan1", "zichan2", "zichan3");CountDownLatch countDownLatch = new CountDownLatch(2);Map<String, Map<String, String>> map = CodeConstant.CODE;for (String type : list) {downLatchService.test(type, map, countDownLatch);}countDownLatch.await();} catch (InterruptedException e) {e.printStackTrace();}logger.info("测试多线程结束...");}}

TestCountDownLatchService 类

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.concurrent.CountDownLatch;@Service
public class TestCountDownLatchService {private static final Logger logger = LoggerFactory.getLogger(TestCountDownLatchService.class);@Asyncpublic void test(String type, Map<String, Map<String, String>> map, CountDownLatch countDownLatch) {Map<String, String> typeMap = map.get(type);if (typeMap == null) {countDownLatch.countDown();return;}try {logger.info("类型{}的信息{}", type, typeMap);for (int i = 0; i < 1000; i++) {for (int j = 0; j < 1000; j++) {logger.debug("i*j={}", i * j);}}} catch (Exception e) {logger.error("类型{}异常:", type, e);} finally {countDownLatch.countDown();}}
}

 启动类上补充注解 @EnableAsync

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;@EnableAsync
@SpringBootApplication
@MapperScan(basePackages = { "com.example.demo" })
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}

测试结果

可以看出,主线程执行"测试多线程开始"后,开启了两个子线程,等待子线程全部完成后,主线程继续执行"测试多线程结束";

相关文章:

  • 简单地聊一聊Spring Boot的构架
  • 百度文心一言
  • redis高级面试
  • 英语语法 - 祈使句 | 虚拟语气
  • Python机器学习算法入门教程(第五部分)
  • Apache Spark 的基本概念和在大数据分析中的应用
  • API 集成测试工具Hitchhiker 0.1.1 正式发布
  • Origin:科研绘图与学术图表绘制从入门到精通
  • 【C/PTA——8.数组2(课外实践)】
  • 家政服务小程序源码系统+上门预约服务 源码完全开源可二次开发 带完整的搭建教程
  • 2.4 Windows驱动开发:内核字符串拷贝与比较
  • github常用搜索指令
  • ROS基础—vscode创建工作空间
  • ARPG----C++学习记录04 Section8 角色类,移动
  • RHCE第四次作业
  • 「面试题」如何实现一个圣杯布局?
  • 5、React组件事件详解
  • iOS | NSProxy
  • python 装饰器(一)
  • Spring Boot快速入门(一):Hello Spring Boot
  • vue总结
  • weex踩坑之旅第一弹 ~ 搭建具有入口文件的weex脚手架
  • 等保2.0 | 几维安全发布等保检测、等保加固专版 加速企业等保合规
  • 飞驰在Mesos的涡轮引擎上
  • 技术发展面试
  • 讲清楚之javascript作用域
  • 利用DataURL技术在网页上显示图片
  • 利用jquery编写加法运算验证码
  • 浅谈Golang中select的用法
  • 设计模式走一遍---观察者模式
  • 深入浏览器事件循环的本质
  • 通信类
  • 优秀架构师必须掌握的架构思维
  • const的用法,特别是用在函数前面与后面的区别
  • 函数计算新功能-----支持C#函数
  • ​LeetCode解法汇总2583. 二叉树中的第 K 大层和
  • ​中南建设2022年半年报“韧”字当头,经营性现金流持续为正​
  • ![CDATA[ ]] 是什么东东
  • (20)目标检测算法之YOLOv5计算预选框、详解anchor计算
  • (30)数组元素和与数字和的绝对差
  • (4)logging(日志模块)
  • (arch)linux 转换文件编码格式
  • (pojstep1.1.2)2654(直叙式模拟)
  • (pojstep1.3.1)1017(构造法模拟)
  • (Redis使用系列) Springboot 使用redis实现接口幂等性拦截 十一
  • (二)JAVA使用POI操作excel
  • (附源码)php投票系统 毕业设计 121500
  • (更新)A股上市公司华证ESG评级得分稳健性校验ESG得分年均值中位数(2009-2023年.12)
  • (力扣题库)跳跃游戏II(c++)
  • (六)vue-router+UI组件库
  • (每日持续更新)jdk api之FileFilter基础、应用、实战
  • (实战篇)如何缓存数据
  • (四) Graphivz 颜色选择
  • (四)【Jmeter】 JMeter的界面布局与组件概述
  • .htaccess配置重写url引擎