创建线程池处理复杂数据,并返回所需参数

		//定义存储处理好数据的批量业务数据集合
        List<BatchProductsBO> batchProductsBOList = new ArrayList<>();
        //定义返回的错误数据的行和列
        Map<String,List<String>> mapAll = new HashMap<>();
        long start = System.currentTimeMillis();
        //批量的业务数据按capacity为一组,执行多线程
        int capacity = 300;
        List<List<BatchProductsBO>> perList = ListUtil.splitListBycapacity(batchProductsBOS, capacity);
        log.info("批量业务开始多线程执行,线程数:{}", perList.size());
        // 创建线程池
        ExecutorService threadPool = Executors.newFixedThreadPool(perList.size());
        for (int i = 0; i < perList.size(); i++) {
            try {
                int finalI = i;
                threadPool.execute(() -> {
                    log.info("本次批量业务数据{},执行{}条数据", finalI, perList.get(finalI).size());
                    Map<String,List<String>> map = dealDataAll(perList.get(finalI),finalI);
                    batchProductsBOList.addAll(perList.get(finalI));
                    mapAll.putAll(map);
                    long perEnd = System.currentTimeMillis();
                    log.info("本次批量业务数据{},执行{}条数据,花费[{}]秒", finalI, perList.get(finalI).size(), (perEnd - start) / 1000);
                });
            } catch (Exception e) {
                log.error("本次批量导入数据[{}]线程执行异常:{}", i, e.getMessage());
            }
        }
        //关闭线程池
        threadPool.shutdown();
        while (true) {
        	//线程池中所有子线程执行结束
            if (threadPool.isTerminated()) {
                log.info("所有的子线程都结束了!");
                if(CollectionUtils.isEmpty(mapAll)){
                    List<BatchProductsPO> batchProductsPOS = JsonUtil.copyList(batchProductsBOList,BatchProductsPO.class);
                    this.saveBatch(batchProductsPOS);
                }
                log.info("所有批量业务数据插入执行完毕");
                break;
            }
        }
        return ResponseBuild.buildSuccResponse(mapAll);
    /**
     * 2> 根据目标容量 划分List
     * @param source
     * @param capacity 划分完成的单个List容量
     * @param <T>
     * @return
     */
    public static <T> List<List<T>> splitListBycapacity(List<T> source,int capacity){
        List<List<T>> result=new ArrayList<List<T>>();
        if (source != null){
            int size = source.size();
            if (size > 0 ){
                for (int i = 0; i < size;) {
                    List<T> value = null;
                    int end = i+capacity;
                    if (end > size){
                        end = size;
                    }
                    value = source.subList(i,end);
                    i = end;
                    result.add(value);
                }
            }else {
                result = null;
            }
        }else {
            result = null;
        }
        return result;
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

壹猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值