Future学习记录

        List<String> serverIds  = tblGameActivityService.queryServersByChannelOrGameId(paramMap); //获取区服数组
        if (serverIds == null){
            Map<String,Object> reqOut=new HashMap<>();
            reqOut.put("total",0);
            reqOut.put("rows",new ArrayList<>());
            return ResponseEntity.ok(reqOut);
        }
        long now = System.currentTimeMillis();
        System.out.println("now==========> " +now);
        //多数据源异步查询
        List<Future<Map<String, Object>>> futureList = new ArrayList<>();
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (String serverId : serverIds){ //异步查询数据总数
            paramMap.put("serverId",serverId);
            futureList.add(executorService.submit(()->{
                Map<String, Object> map = new HashMap<>();
                map.put("serverId",serverId);
                System.out.println("Future==========> " +serverId);
                map.put("num",logService.countItemLog(paramMap));
                        return map;//当前区服总数据量
                    }));
        }
  		List<Map<String, Object>> list = new ArrayList<>();
        Integer dataStart = Integer.valueOf(jsonObject.get("offset").toString()); //当前起始数据index
        int count = 0; //记录数据总数
        int remainder = 0; //前一个区服循环总数
        boolean flg = true;
        for (Future<Map<String, Object>> future : futureList){
            try {
                Map<String, Object> map = future.get();
                paramMap.put("serverId",map.get("serverId"));
                Integer num = Integer.parseInt(map.get("num").toString());
                remainder = count;
                count+=num;
                System.out.println("server=> "+ map.get("serverId") +" num=> " + num + " remainder=> " + remainder + " count=> " + count + " dataStart=> " + dataStart);
                Integer limit = Integer.valueOf(jsonObject.get("limit").toString());

                if (dataStart < count && flg){ //当前页起始index 小于当前总数据时拉取详细数据
                    int offset = dataStart - remainder; //上一区查询数据剩余量差值
                    limit = offset < 0 ? limit+offset : limit; //若上小于0,上一区服当前页数据未满 本次查询补余量
                    paramMap.put("offset",offset < 0 ? 0 : offset);
                    paramMap.put("limit", limit);
                    System.out.println("offset=> "+ offset +" limit=> " + limit );
                    List<Map<String, Object>> logList = logService.queryItemLog(paramMap);
                    list.addAll(logList);
                    if (limit == logList.size()){ //查出数据若等于lim 当前页数据已拉完不再查询数据
                        flg = false;
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值