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

线程池 同时多表查询返回结果集

@Configuration public class ThreadPoolConfig { // Spring 的线程池 @Bean public ThreadPoolTaskExecutor threadPoolTaskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(10); executor.setMaxPoolSize(20); executor.setQueueCapacity(200); executor.setThreadNamePrefix("MyExecutor-"); executor.initialize(); return executor; } } -------------------- @RestController public class MyController { @Autowired private ThreadPoolTaskExecutor executor; @GetMapping("/async") public String asyncMethod() { executor.execute(() -> { // 异步操作 }); return "success"; } } -------------------- 或者 @Autowired @Qualifier("threadPoolTaskExecutor") private ThreadPoolTaskExecutor threadPoolTaskExecutor; StudyPlanVo vo = new StudyPlanVo(); CompletableFuture<Void> basicFuture = CompletableFuture.runAsync(() -> { StudyPlanBasic basic = studyPlanBasicMapper.findByTaskId(taskDataId); vo.setBasic(basic); }, threadPoolExecutor).exceptionally(ex -> { log.error("Failed to studyPlanBasicMapper.findByTaskId", ex); return null; }); CompletableFuture<Void> moleculeFuture = CompletableFuture.runAsync(() -> { List<StudyPlanMolecule> moleculeList = studyPlanMoleculeMapper.findByTaskId(taskDataId); vo.setMoleculeList(moleculeList); }, threadPoolExecutor).exceptionally(ex -> { log.error("Failed to studyPlanMoleculeMapper.findByTaskId", ex); return null; }); CompletableFuture.allOf(basicFuture, moleculeFuture).get(1L, TimeUnit.MINUTES);
@Configuration // 启用异步任务,guava ThreadFactoryBuilder public class AsyncConfiguration { final int cpuNum = Runtime.getRuntime().availableProcessors(); // java 的线程池 @Bean("threadPoolExecutor") public ThreadPoolExecutor threadPoolExecutor() { final ThreadFactory queueThreadFactory = new ThreadFactoryBuilder() .setNameFormat("queue-pool-%d").build(); return new ThreadPoolExecutor(cpuNum, cpuNum*3, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(120), queueThreadFactory, new ThreadPoolExecutor.CallerRunsPolicy()); } } @Autowired @Qualifier("threadPoolExecutor") private ThreadPoolExecutor threadPoolExecutor; ------------------------------------- public StudyPlanVo studyPlan(String taskDataId) throws Exception { StudyPlanVo vo = new StudyPlanVo(); CompletableFuture<Void> basicFuture = CompletableFuture.runAsync(() -> { StudyPlanBasic basic = studyPlanBasicMapper.findByTaskId(taskDataId); vo.setBasic(basic); }, threadPoolExecutor).exceptionally(ex -> { log.error("Failed to studyPlanBasicMapper.findByTaskId", ex); return null; }); CompletableFuture<Void> moleculeFuture = CompletableFuture.runAsync(() -> { List<StudyPlanMolecule> moleculeList = studyPlanMoleculeMapper.findByTaskId(taskDataId); vo.setMoleculeList(moleculeList); }, threadPoolExecutor).exceptionally(ex -> { log.error("Failed to studyPlanMoleculeMapper.findByTaskId", ex); return null; }); CompletableFuture<Void> prescriptionFuture = CompletableFuture.runAsync(() -> { List<StudyPlanPrescription> prescriptionList = studyPlanPrescriptionMapper.findByTaskId(taskDataId); if (!CollectionUtils.isEmpty(prescriptionList)) { prescriptionList.sort((o1, o2) -> { long a1 = parseSequence(o1.getSequenceNumber()); long a2 = parseSequence(o2.getSequenceNumber()); return Long.compare(a1, a2); }); } vo.setPrescriptionList(prescriptionList); }, threadPoolExecutor).exceptionally(ex -> { log.error("Failed to studyPlanPrescriptionMapper.findByTaskId", ex); return null; }); CompletableFuture<Void> testGroupFuture = CompletableFuture.runAsync(() -> { List<StudyPlanTestGroup> testGroupList = studyPlanTestGroupMapper.findByTaskId(taskDataId); vo.setTestGroupList(testGroupList); }, threadPoolExecutor).exceptionally(ex -> { log.error("Failed to studyPlanTestGroupMapper.findByTaskId", ex); return null; }); CompletableFuture<Void> conditionFuture = CompletableFuture.runAsync(() -> { List<StudyPlanCondition> conditionList = studyPlanConditionMapper.findByTaskId(taskDataId); vo.setConditionList(conditionList); }, threadPoolExecutor).exceptionally(ex -> { log.error("Failed to studyPlanConditionMapper.findByTaskId", ex); return null; }); CompletableFuture<Void> proteinFuture = CompletableFuture.runAsync(() -> { List<StudyPlanProtein> proteinList = studyPlanProteinMapper.findByTaskId(taskDataId); vo.setProteinList(proteinList); }, threadPoolExecutor).exceptionally(ex -> { log.error("Failed to studyPlanProteinMapper.findByTaskId", ex); return null; }); CompletableFuture<Void> reagentFuture = CompletableFuture.runAsync(() -> { List<StudyPlanReagent> reagentList = studyPlanReagentMapper.findByTaskId(taskDataId); vo.setReagentList(reagentList); }, threadPoolExecutor).exceptionally(ex -> { log.error("Failed to studyPlanReagentMapper.findByTaskId", ex); return null; }); CompletableFuture<Void> consumableFuture = CompletableFuture.runAsync(() -> { List<StudyPlanConsumable> consumableList = studyPlanConsumableMapper.findByTaskId(taskDataId); vo.setConsumableList(consumableList); }, threadPoolExecutor).exceptionally(ex -> { log.error("Failed to studyPlanConsumableMapper.findByTaskId", ex); return null; }); CompletableFuture<Void> numFuture = CompletableFuture.runAsync(() -> { List<StudyPlanNum> numList = studyPlanNumMapper.findByTaskId(taskDataId); vo.setNumList(numList); }, threadPoolExecutor).exceptionally(ex -> { log.error("Failed to studyPlanNumMapper.findByTaskId", ex); return null; }); CompletableFuture.allOf(basicFuture, moleculeFuture, prescriptionFuture, testGroupFuture, conditionFuture, proteinFuture, reagentFuture, consumableFuture, numFuture) .get(1L, TimeUnit.MINUTES); return vo; }
import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; public class CompletableFutureAllOfDemo { public static void main(String[] args) throws ExecutionException, InterruptedException { // 任务A:模拟耗时操作(如日志打印) CompletableFuture<Void> taskA = CompletableFuture.runAsync(() -> { System.out.println("任务A执行中..."); sleep(1000); // 模拟耗时1秒 System.out.println("任务A执行完毕"); }); // 任务B:模拟数据处理 CompletableFuture<Void> taskB = CompletableFuture.runAsync(() -> { System.out.println("任务B执行中..."); sleep(1500); // 模拟耗时1.5秒 System.out.println("任务B执行完毕"); }); // 任务C:模拟文件上传 CompletableFuture<Void> taskC = CompletableFuture.runAsync(() -> { System.out.println("任务C执行中..."); sleep(800); // 模拟耗时0.8秒 System.out.println("任务C执行完毕"); }); // 任务D:模拟接口调用 CompletableFuture<Void> taskD = CompletableFuture.runAsync(() -> { System.out.println("任务D执行中..."); sleep(1200); // 模拟耗时1.2秒 System.out.println("任务D执行完毕"); }); // 关键:用 allOf 组合所有任务,等待全部完成 CompletableFuture<Void> allTasks = CompletableFuture.allOf(taskA, taskB, taskC, taskD); // 方式1:阻塞等待所有任务完成(适合主线程需要等待结果的场景) allTasks.join(); // join() 抛出 unchecked 异常(无需显式捕获) // 或 allTasks.get(); // get() 抛出 checked 异常(需 try-catch) System.out.println("\n===== 所有任务(A/B/C/D)执行完毕 ====="); // 方式2:非阻塞注册“所有任务完成后执行的逻辑”(适合回调场景) allTasks.thenRun(() -> { System.out.println("\n===== 回调:所有任务(A/B/C/D)执行完毕 ====="); // 这里可以写后续逻辑(如汇总结果、触发下一个流程) }).join(); // 若需等待回调执行,可再加 join() } // 辅助方法:模拟耗时 private static void sleep(long millis) { try { Thread.sleep(millis); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } }
http://www.jsqmd.com/news/446908/

相关文章:

  • 超星学习通使用笔记
  • 别等故障来了才救火:聊聊如何用 AI 把 SLA 这件事“提前做对”
  • 显示学习6(DRM)(TODO)
  • 物联网之Arduino开发环境的下载与安装、ESP32开发环境的下载与安装、常见环境配置问题的解决办法、COM端口不可用的解决方法
  • vue+element纯手工完美模拟实现小米有品网站
  • Springboot整合RabbitMQ
  • uview plus u-calendar日历设置部分日期不可选择disabled
  • 系统不出事,才是运维的最高境界:聊聊如何打造“零故障运维系统”
  • electron 开发轻量级本地数据存储桌面端应用(简洁版)
  • 【100%通过率】华为od统一考试B卷【流水线调度 / 自动化维修流水线】JavaScript 实现
  • Web前端之Css网格布居中的动画、VSC中Scss自动编译成Css、通过子元素改变父元素的样式值、安装和配置Sass插件、样式特殊单位、hover、child、grid、nth、fr
  • minio 安装部署、主从、卸载、基础使用
  • 数据库同步软件,PanguSync霸气!!!
  • AQS原理
  • 写文章,得有点儿原则
  • Web前端之UniApp低功耗蓝牙一键开门、数组匹配数组、多对多查找、开锁
  • Labelme安装以及qt.qpa.plugin: Could not load the Qt platform plugin “xcb“ in ““ even though it was f问题解决
  • CAS原理
  • 微信小程序TS+SASS使用vant导致体验版白屏SystemError (jsEnginScriptError) X(...).bem is not a function
  • 【节点】[Fog节点]原理解析与实际应用
  • 酷炫 css 按钮 边框霓虹
  • 软件设计师考试中需要掌握的一些常用算法,基于C++实现
  • Mybatis的延迟加载
  • 教程 | 如何动用智慧安装NavicatPremium 16
  • Python 潮流周刊#141:Python 早期贡献者口述历史
  • Web前端之旋转木马的图片效果、鼠标进入停止动画、keyframes、hover、child、nth
  • canvas_3_绘制弧形
  • linux-centos常用指令、tar.gz解压、mv重命名、cp复制、ss -ltnp、curl测试任意端口网络是否可达等
  • 女生必看!用OpenClaw龙虾当你的24小时免费助理,职场、生活效率翻倍,做自己的女王!
  • 2026年宜昌两天一夜游路线权威榜单:十大精品路线深度评测与排位赛 - 品牌推荐