java 解决多次嵌套循环查询数据库导致代码中数据处理慢的问题

业务现象:代码中有一部分代码多次嵌套循环和数据处理,执行速度很慢

解决方案:通过多线程

1:启用多线程

private final static Executor executor = Executors.newFixedThreadPool(3);

2、初始化设置count,即等待(await)count个线程或一个线程count次计数,通过工作线程来countDown计数减一,直到计数为0,await阻塞结束;目的:保证所有线程都走完
  final CountDownLatch latch = new CountDownLatch(dataList.size());

3、需要重新run .需要多线程的代码写在run中
 @Override
       public void run() { 

                //业务代码处理

                //countDown计数减一

                 latch.countDown();

}


4、阻塞线程

  // 等待所有工作线程结束
            latch.await();

关键代码:

 private final static Executor executor = Executors.newFixedThreadPool(3);//启用多线程

 public Result getList(@RequestBody StatusDbSelectParam param){

PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<Map<String, Object>> dataList = statusDbService.selectByTime(tableName,             columnNames.toString(), param.getStartTime(), param.getEndTime());
        //初始化设置count,即等待(await)count个线程或一个线程count次计数,通过工作线程来countDown计数减一,直到计数为0,await阻塞结束
        final CountDownLatch latch = new CountDownLatch(dataList.size());
        for(int k =0;k<dataList.size();k++) {
            final int i = k;
            String finalTableName = tableName;
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Map<String, Object> map = dataList.get(i);
                        for (String pid : map.keySet()) {
                            String val = String.valueOf(map.get(pid));
                            String columPid = pid.substring(2, pid.length()).toLowerCase();
                            List<MonitorRuleEntity> list = monitorRuleService.listMonitorRule(columPid, finalTableName);
                            String status = "";
                            //循环规则数据 判断监测点是否报警 更新状态
                            for (int j = 0; j < list.size(); j++) {
                                Boolean flag = DevHealthStatusFactory.getInstance().getResultMapByRule(val, list.get(j));
                                if (flag) {
                                    if (j == 0) {
                                        status = "A";
                                        break;
                                    } else if (j == 1) {
                                        status = "B";
                                        break;
                                    } else if (j == 2) {
                                        status = "C";
                                        break;
                                    }
                                }
                            }
                            if (pid.equalsIgnoreCase(timeColumnName)) {
                                map.put(pid, val);
                            } else {
                                map.put(pid, val + "_" + status);
                            }
                        }
                        latch.countDown();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        try {
            // 等待所有工作线程结束
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return ResultMsg.successMsg().data(new PageInfo<>(dataList));
    }

  • 5
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值