欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > 多线程执行大批量数据查询

多线程执行大批量数据查询

2025/2/26 0:32:00 来源:https://blog.csdn.net/Joe_Wang1/article/details/145319468  浏览:    关键词:多线程执行大批量数据查询
   // 创建一个固定大小的线程池ExecutorService executorService = Executors.newFixedThreadPool(5);// 创建多个查询任务List<Callable<List<ShopCompareBase>>> tasks = new ArrayList<>();//查询门店 切割,分成十份List<List<String>> shopIdList = averageAssign(reqShopCodes, 10);if (CollectionUtils.isNotEmpty(shopIdList)) {for (List<String> list : shopIdList) {if (CollectionUtils.isNotEmpty(list)) {//xgj->映射门店idMap<String, List<String>> var1 = list.stream().filter(storeIdMap::containsKey) // 过滤掉不存在的 key.collect(Collectors.toMap(key -> key, storeIdMap::get));//三方映射门店idList<String> idList = var1.values().stream().flatMap(List::stream) // 将每个 List<String> 扁平化为 Stream<String>.collect(Collectors.toList());if (CollectionUtils.isEmpty(idList)) {continue;}//转换大数据orgCodeList<String> shopIds = dimMchtHllShopDfService.convertShopId2OrgCode(idList, new ArrayList<>());BusinessDataTendencyRequest req = new BusinessDataTendencyRequest();BeanUtils.copyProperties(request, req);req.setShopIDs(shopIds);storeRankList.addAll(getStoreRank(req));Long t1 = System.currentTimeMillis();log.info("getRealTimeDateBatchQuery.start:{}", System.currentTimeMillis());//多任务执行tasks.add(()-> {StoreBusinessContrastReqDto storeBusinessContrastReqDto = new StoreBusinessContrastReqDto();BeanUtils.copyProperties(request, storeBusinessContrastReqDto);storeBusinessContrastReqDto.setShopIDs(shopIds);return hisOrderRealTimeDataService.getRealTimeDateBatchQuery(dataType, storeBusinessContrastReqDto, request.getIsHomepage());});Long t2 = System.currentTimeMillis();log.info("getRealTimeDateBatchQuery.end 耗时:{}s", (t2 - t1) / 1000.0);}}}try {// 提交所有任务并等待它们完成List<Future<List<ShopCompareBase>>> futures = executorService.invokeAll(tasks);// 获取每个任务的结果for (Future<List<ShopCompareBase>> future : futures) {shopCompareBaseList.addAll(future.get());}} catch (InterruptedException | ExecutionException e) {e.printStackTrace();} finally {// 关闭线程池executorService.shutdown();}
}

/*** 将一个list均分成n个list,主要通过偏移量来实现的** @param source* @return*/
public static <T> List<List<T>> averageAssign(List<T> source, int n) {if(CollectionUtils.isEmpty(source)) return  null;List<List<T>> result = new ArrayList<List<T>>();int remaider = source.size() % n; //(先计算出余数)int number = source.size() / n; //然后是商int offset = 0;//偏移量for (int i = 0; i < n; i++) {List<T> value = null;if (remaider > 0) {value = source.subList(i * number + offset, (i + 1) * number + offset + 1);remaider--;offset++;} else {value = source.subList(i * number + offset, (i + 1) * number + offset);}result.add(value);}return result;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词