Redis + ThreadPoolExecutor 处理大规模数据

    public List<String> getOvertimePeople(Long groupId) {

        String redisUserIds = this.redisTemplate.opsForValue().get(groupId + OVERTIME_USER_ID);
        if (!StringUtils.isEmpty(redisUserIds)) {
            List<String> strings = JSON.parseArray(redisUserIds, String.class);
            return strings;
        } else {

            List<Map<String, Object>> overTimeData = studentInfoVoRepository.findOverTimeData(groupId);
            List<String> accounts = studentInfoVoRepository.findUserIds(groupId);


            List<String> resultUserIds = new ArrayList<>();
            List<String> dates = new ArrayList<>();
            List accountList = new ArrayList<>();
            try {
                List<AccountDate> accountDates = RainMUtils.convertListMapToListBean(overTimeData, AccountDate.class);

                List<List<String>> partitionAccount = Lists.partition(accounts, 50);
                accountDates.stream().forEach(m -> { dates.add(m.getExitDate()); });
                List<String> setDates = dates.parallelStream().distinct().collect(Collectors.toList());

                ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(50, 50, 0L,
                        TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(10), new ThreadPoolExecutor.DiscardPolicy());

                for (int i = 0; i < partitionAccount.size(); i++) {
                    final int num = i;
                    Runnable task = new Runnable() {
                        @Override
                        public void run() {
                            try {
                                List<String> minAccount = partitionAccount.get(num);
                                List<AccountDate> accountDates1 = new ArrayList<>();
                                minAccount.stream().forEach(min -> {
                                    List<AccountDate> collect = accountDates.parallelStream().filter(m -> min.equals(m.getAccount())).collect(Collectors.toList());
                                    accountDates1.addAll(collect);
                                });
                                minAccount.stream().forEach(account -> {
                                    setDates.stream().forEach(date -> {
                                        List<Integer> courseIds = new ArrayList<>();
                                        accountDates1.stream().filter(m -> account.equals(m.getAccount()))
                                                .filter(m1 -> date.equals(m1.getExitDate()))
                                                .forEach(accountDate -> {
                                                    courseIds.add(accountDate.getCourseId());
                                                });
                                        if (Constants.limit_Number_Lessons_PerDay < courseIds.parallelStream().distinct().collect(Collectors.toList()).size()) {
                                            resultUserIds.add(account);
                                        }
                                        courseIds.clear();

                                    });
                                });
                            } catch (Exception e) {
                                e.printStackTrace();
                                throw new BusinessException("分析数据多线程出错", ResultCode.FAILURE);
                            }
                        }
                    };
                    threadPoolExecutor.submit(task);
                }
                threadPoolExecutor.shutdown();
                threadPoolExecutor.awaitTermination(1, TimeUnit.DAYS);
                System.gc();
                accountList = resultUserIds.parallelStream().distinct().collect(Collectors.toList());

                this.redisTemplate.opsForValue().set(groupId + OVERTIME_USER_ID, accountList.toString(), 1, TimeUnit.DAYS);
                return accountList;
            }catch (Exception e) {
 //               e.printStackTrace();
                throw new SelfException("数据统计错误", ResultCode.BAD);
            }
        }

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值