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

还不会使用多线程优化代码执行效率?codefun教你在业务场景中使用CompletableFuture进行优化!

业务场景

我们先来从场景入手,具体的业务是这样的:我们需要从某的省的id去查询这个省份所有的县区,至于什么是县区呢?在DB中我们是这样定义的,也就是字段level = 3 的时候,就代表一个县的信息,然后呢:我们需要查询某个省份的所有县区的项目数,一个县区下可能有很多项目,也可能没有项目信息;

返回格式

{"msg": "操作成功","code": 200,"data": [{"pid": 2,"id": 4,"areaCode": "540121","projectCount": 1,"areaName": "某某县","investMoney": 0,"level": 3,"listMigSupportProjectItemVo": null,"migSupportProjectList": [{"areaCode": "540121","name": "测试01","reservoirs": "1","projectNum": "2","investPlan": "20000.0","type": "散居移民基础设施完善","status": "plan","acceptTime": null,"unitAfterHandover": null}]},{"pid": 23,"id": 24,"areaCode": "542221","projectCount": 3,"areaName": "乃某某某某县","investMoney": 0,"level": 3,"listMigSupportProjectItemVo": null,"migSupportProjectList": [{"areaCode": "542221","name": "项目A","reservoirs": "水库A","projectNum": "项目批准文号A","investPlan": "1000000.0","type": "就业创业能力建设","status": "implemented","acceptTime": "2023-02-01","unitAfterHandover": "单位B"},{"areaCode": "542221","name": "项目B","reservoirs": "水库B","projectNum": "项目批准文号B","investPlan": "2000000.0","type": "类型2","status": "implemented","acceptTime": "2024-03-01","unitAfterHandover": "孤帆候风进"},{"areaCode": "542221","name": "项目C","reservoirs": "水库C","projectNum": "项目批准文号C","investPlan": "3000000.0","type": "类型3","status": "implementation","acceptTime": null,"unitAfterHandover": null}]},{"pid": 23,"id": 25,"areaCode": "542222","projectCount": 2,"areaName": "扎囊县","investMoney": 0,"level": 3,"listMigSupportProjectItemVo": null,"migSupportProjectList": [{"areaCode": "542222","name": "项目D","reservoirs": "水库D","projectNum": "项目批准文号D","investPlan": "4000000.0","type": "类型1","status": "plan","acceptTime": "2026-03-01","unitAfterHandover": "单位D"},{"areaCode": "542222","name": "项目F","reservoirs": "水库F","projectNum": "项目批准文号F","investPlan": "6000000.0","type": "类型2","status": "plan","acceptTime": null,"unitAfterHandover": null}]},{"pid": 23,"id": 30,"areaCode": "542227","projectCount": 2,"areaName": "某某1县","investMoney": 0,"level": 3,"listMigSupportProjectItemVo": null,"migSupportProjectList": [{"areaCode": "542227","name": "项目E","reservoirs": "水库E","projectNum": "项目批准文号E","investPlan": "5000000.0","type": "类型2","status": "plan","acceptTime": "2023-07-08","unitAfterHandover": "规范打广告"},{"areaCode": "542227","name": "1111","reservoirs": "1","projectNum": "1","investPlan": "10000.0","type": null,"status": "plan","acceptTime": null,"unitAfterHandover": null}]}]
}

原始代码

具体的话呢,会返回这样的一个Json格式,可以看出,没有项目的县是不会被返回的,并且查询出来的项目信息回做一个合并的统计,具体的场景就是查询所有某个省所有level为3的信息,然后拿到它的区域编码然后去项目表中查询项目信息,也就是我们要执行很多查询的sql,根据区域编码去查询项目集合(每一个县区的编码都不一样),接下来看一下我们开始写的代码

public List<MigSupportProjectScreenVo> selectAllisBuildProjectByLowestLevel(MigSupportScreenBo migSupportScreenBo) {//先查询所有level = 3 的区域数据List<MigSupportProjectScreenVo> migSupportProjectScreenVos = screenSupportMapper.selectByLowestLevel();migSupportProjectScreenVos.forEach(item -> {item.setMigSupportProjectList(selectProjectListByid(item.getAreaCode(), migSupportScreenBo.getType()));});ArrayList<MigSupportProjectScreenVo> res = new ArrayList<>();migSupportProjectScreenVos.forEach(item->{if (item.getMigSupportProjectList().size()!=0){res.add(item);}});return res;
}//根据区域的code查询一个项目集合
private List<MigSupportProject> selectProjectListByid(String code, Integer type) {return migSupportProjectMapper.selectProjectType(code, type);
}

是单线程的代码,在循环里面调用sql,然后在APIFox中进行测试,在清除了Mybatis缓存的情况下
在这里插入图片描述

优化后的代码

经过优化,我们用了CompletableFuture进行性能的优化

public List<MigSupportProjectScreenVo> selectAllisBuildProjectByLowestLevel(MigSupportScreenBo migSupportScreenBo) {// 先查询所有level = 3 的区域数据List<MigSupportProjectScreenVo> migSupportProjectScreenVos = screenSupportMapper.selectByLowestLevel();// 使用 CompletableFuture 异步执行 selectProjectListByid 方法List<CompletableFuture<Void>> futures = new ArrayList<>();for (MigSupportProjectScreenVo item : migSupportProjectScreenVos) {CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {item.setMigSupportProjectList(selectProjectListByid(item.getAreaCode(), migSupportScreenBo.getType()));return null;});futures.add(future);}// 等待所有异步任务完成CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();// 过滤掉没有项目的 MigSupportProjectScreenVo 对象List<MigSupportProjectScreenVo> res = migSupportProjectScreenVos.stream().filter(item -> item.getMigSupportProjectList().size() != 0).collect(Collectors.toList());return res;
}

使用forkerJoinPool的默认线程池进行优化,没有使用自定义线程池.
在这里插入图片描述
由此可见,速度肉眼可见的提升了!

http://www.lryc.cn/news/360067.html

相关文章:

  • 数据结构-堆(带图)详解
  • React Native 之 react-native-share(分享)库 (二十三)
  • JCR一区级 | Matlab实现TCN-BiGRU-MATT时间卷积双向门控循环单元多特征分类预测
  • 游戏心理学Day01
  • 错误模块路径: ...\v4.0.30319\clr.dll,v4.0.30319 .NET 运行时中出现内部错误,进程终止,退出代码为 80131506。
  • 005 CentOS 7.9 RabbitMQ安装及配置
  • Xcode 15 libarclite 缺失问题
  • 绘画智能体分享
  • 7_2、C++程序设计进阶:数据共享
  • d2-crud-plus 使用小技巧(五)—— 搜索时间(或下拉列表)后,点击X清除按钮后返回值为null,导致异常
  • ChatGPT成知名度最高生成式AI产品,使用频率却不高
  • R19 NR移动性增强概况
  • C语言:如何写文档注释、内嵌注释、行块注释?
  • Turtle中circle用法详解
  • stack和queue(1)
  • 前端3剑客(第1篇)-初识HTML
  • 植被变化趋势线性回归以及可视化
  • 大话设计模式学习笔记
  • MiniMax公司介绍
  • lucene 9.10向量检索基本用法
  • 【2023百度之星初赛】跑步,夏日漫步,糖果促销,第五维度,公园,新材料,星际航行,蛋糕划分
  • vs2019 QT UI 添加新成员或者控件代码不提示问题解决方法
  • 【面试八股总结】MySQL事务:事务特性、事务并行、事务的隔离级别
  • STL用法总结
  • 他人项目二次开发——慎接
  • k8s之PV、PVC
  • 新人学习笔记之(JavaScript作用域)
  • 图论第一天
  • 革新风暴来袭:报事报修系统小程序如何重塑报事报修体验?
  • linux各个日志的含义 以及使用方法