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

发布于:2025-02-10 ⋅ 阅读:(39) ⋅ 点赞:(0)
   // 创建一个固定大小的线程池
    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->映射门店id
                Map<String, List<String>> var1 = list.stream()
                        .filter(storeIdMap::containsKey) // 过滤掉不存在的 key
                        .collect(Collectors.toMap(key -> key, storeIdMap::get));
               //三方映射门店id
                List<String> idList = var1.values().stream()
                        .flatMap(List::stream) // 将每个 List<String> 扁平化为 Stream<String>
                        .collect(Collectors.toList());
                if (CollectionUtils.isEmpty(idList)) {
                    continue;
                }
                //转换大数据orgCode
                List<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;
}

网站公告

今日签到

点亮在社区的每一天
去签到