批量化异步执行线程
@Resource private AsyncTaskExecutor taskExecutor; // 使用 CompletableFuture 并行执行 enterpriseInfoMap 和 waybillVoList 的获取 CompletableFuture<Map<String, Object>> enterpriseInfoMapFuture = CompletableFuture.supplyAsync(() -> sysEnterpriseInfoService.businessTypeStatistic(finalXzqhPrefix), taskExecutor); CompletableFuture<List<OdsBinlogTrafficWaybillVo>> waybillVoListFuture = CompletableFuture.supplyAsync(() -> odsWaybillService.getAvalibleSimpleWaybillList(finalXzqhPrefix), taskExecutor); // 用于存储 carHphmList、jsyVocationNumList 和 yyyIdCardList 的 CompletableFuture CompletableFuture<List<String>> carHphmListFutureHolder = new CompletableFuture<>(); CompletableFuture<List<String>> jsyVocationNumListFutureHolder = new CompletableFuture<>(); CompletableFuture<List<String>> yyyIdCardListFutureHolder = new CompletableFuture<>(); // 在 enterpriseInfoMapFuture 完成后,执行 carHphmList、jsyVocationNumList 和 yyyIdCardList 的获取 CompletableFuture<Void> dependentTasks = enterpriseInfoMapFuture.thenAcceptAsync(enterpriseInfoMap -> { Set<Long> entIdSet = (Set<Long>) enterpriseInfoMap.get("idSet"); CompletableFuture<List<String>> carHphmListFuture = CompletableFuture.supplyAsync(() -> ywJdcXxService.selectHphmListByEntId(entIdSet), taskExecutor); // 需要适配 CompletableFuture<List<String>> jsyVocationNumListFuture = CompletableFuture.supplyAsync(() -> ywJsyXxService.selectYwJsyIdCardListByEntId(entIdSet), taskExecutor); // 需要适配 CompletableFuture<List<String>> yyyIdCardListFuture = CompletableFuture.supplyAsync(() -> ywYyyXxService.selectYwYyyIdCardListByEntId(entIdSet), taskExecutor); // 需要适配 // 将结果设置到外部可访问的 CompletableFuture 中 carHphmListFuture.thenAccept(carHphmListFutureHolder::complete); jsyVocationNumListFuture.thenAccept(jsyVocationNumListFutureHolder::complete); yyyIdCardListFuture.thenAccept(yyyIdCardListFutureHolder::complete); }, taskExecutor); // 需要适配 // 等待所有必要的 future 完成 CompletableFuture<Void> allFutures = CompletableFuture.allOf( enterpriseInfoMapFuture, waybillVoListFuture, dependentTasks, // 等待 dependentTasks 完成,但它不返回结果 carHphmListFutureHolder, // 确保这些也完成 jsyVocationNumListFutureHolder, yyyIdCardListFutureHolder ); // 获取结果 allFutures.get(); // 等待所有任务完成 Map<String, Object> enterpriseInfoMap = enterpriseInfoMapFuture.get(); List<String> carHphmList = carHphmListFutureHolder.get(); List<String> jsyVocationNumList = jsyVocationNumListFutureHolder.get(); List<String> yyyIdCardList = yyyIdCardListFutureHolder.get(); List<OdsBinlogTrafficWaybillVo> waybillVoList = waybillVoListFuture.get();
AI大火腿 :痛苦预示着超脱
本文来自博客园,作者:AI大火腿,转载请注明原文链接:https://www.cnblogs.com/supperlhg/p/18842739