记录一次用线程池解决上亿数量的查询功能

由于mysql承载的数据量有限所以这个表是按照月份进行水平分表

第一步 通过传入的起始年月切割成按照月份的集合然后再通过偏移量来把集合切分成5个集合List<List> codeList = Utils.averageAssign(yearMonthParam, poolSize);

第二步 初始化线程池 初始化计数器
ExecutorService pool = Executors.newFixedThreadPool(poolSize);
CountDownLatch latch = new CountDownLatch(poolSize);

第三步 循环外层集合,因为数据量大分5次获取,外层集合中开始线程池然后循环获取每个月数据

第四步 暂停当前线程,死循环 判断线程数是否结束

 public List<ReceiveDetPojo> queryReceiveDel(QueryReceiveDetReq param) {
    List<ReceiveDetPojo> pojos = Collections.synchronizedList(new ArrayList(200000));
    List<String> yearMonthParam = getMonthBetween(getYearMonth(param.getReceiveStartDate()), getYearMonth(param.getReceiveEndDate()));
    int poolSize = 5;
    // 初始化线程池
    ExecutorService pool = Executors.newFixedThreadPool(poolSize);
    // 初始化计数器
    CountDownLatch latch = new CountDownLatch(poolSize);
    List<List<String>> codeList = Utils.averageAssign(yearMonthParam, poolSize);
    for (List<String> codes : codeList) {
        pool.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    // 将结果汇总
                    for (int i = 0; i < codes.size(); i++) {
                        QueryReceiveDetReq queryReceiveDetReq = new QueryReceiveDetReq();
                        BeanUtils.copyProperties(param, queryReceiveDetReq);
                        queryReceiveDetReq.setPeriodMonth(codes.get(i));
                        pojos.addAll(getReceivedDelList(queryReceiveDetReq));
                    }
                } catch (Exception e) {
                    logger.error("查询数据异常" + e);
                } finally {
                    //线程结束-1
                    latch.countDown();
                }
            }
        });
    }
    // 等待所有查询结束
    //暂停当前线程,死循环 判断线程数是否结束
    try {
        latch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
        throw new BusinessException("线程结束异常");
    }
    if (CollectionUtils.isEmpty(pojos)){
         return Collections.EMPTY_LIST;
     }
    return pojos;
}
 
public static List<List<String>> averageAssign(List<String> source, int n) {
    List<List<String>> result = new ArrayList<>();
    int remaider = source.size() % n;  //(先计算出余数)
    int number = source.size() / n;  //然后是商
    int offset = 0;//偏移量
    for (int i = 0; i < n; i++) {
        List<String> 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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值