HeavenTang

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

CountDownLatch

// 批量修改
        if (CollectionUtils.isNotEmpty(updateKsxxList)) {
            int oneBatch = MagicNumber.ONE_HUNDRED;
            int times = updateKsxxList.size() % oneBatch == MagicNumber.ZERO ? (updateKsxxList.size() / oneBatch)
                    : ((updateKsxxList.size() / oneBatch) + 1);
            final CountDownLatch countDownLatch = new CountDownLatch(times);
            for (int i = MagicNumber.ZERO; i < times; i++) {
                List<Ksxx> updateList = new ArrayList<>();
                if (i == (times - 1)) {
                    updateList = updateKsxxList.subList(i * oneBatch, updateKsxxList.size());
                } else {
                    updateList = updateKsxxList.subList(i * oneBatch, (i + 1) * oneBatch);
                }
                List<Ksxx> finalUpdateList = updateList;
                new Thread(new Runnable() {
                    public void run() {
                        ksxxMapper.updateBatchAll(finalUpdateList);
                        countDownLatch.countDown();
                    }
                }).start();
            }
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                log.error(e.getMessage());
            }
        }
点击查看代码

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

private void defenseReview(AtomicInteger ret, List<ApprovalResultVo> approvalResultVos) {
        long startTime = System.currentTimeMillis();
        //对答辩资格审核数据进行处理,如果有对应专业的按专业标准,没有的按统一标准进行过滤
        List<ApprovalResultVo> approvalResultVoList = approvalResultVos.stream()
                .collect(Collectors.groupingBy(ApprovalResultVo::getApprovalResultId)).values().stream().map(
                        resultVos ->  resultVos.stream().max(Comparator.comparing(ApprovalResultVo::getMajorCode)).orElse(null)
                ).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(approvalResultVoList)) {
            // 分组,每组xx人
            List<List<ApprovalResultVo>> listGroup = SplitListUtils.subList(approvalResultVoList, 20);
            // 线程池,开启100个线程,每条线程处理xx个人数据
            ExecutorService executorService = Executors.newFixedThreadPool(100);
            final CountDownLatch countDownLatch = new CountDownLatch(listGroup.size());
            listGroup.forEach(voList -> executorService.execute(() -> {
                try {
                    voList.forEach(vo -> {
                        // 获取学号
                        String studentId = vo.getStudentId();
                        // 根据学号查询标准
                        ApprovalStandardVo standard = queryStandard(studentId);
                        // 查看标准是否存在
                        if (Objects.isNull(standard)) {
                            log.info("学号【{}】,学生年级的审核标准为空!,当前时间{}", studentId, DateUtil.now());
                        } else {
                            ApprovalResultVo newVo = compareAndReplace(standard, vo);
                            // 根据 vo 更新答辩结果表
                            ret.addAndGet(departmentReviewMapper.sync(newVo));
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error("刷新网络审核结果出错:{}", e.getMessage());
                } finally {
                    countDownLatch.countDown();
                }
            }));

            try {
                // 等待闭锁减为0,才执行下面的操作
                countDownLatch.await();
                // 关闭线程池
                executorService.shutdown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            long endTime = System.currentTimeMillis();
            log.info("答辩资格审核刷新成功!刷新{}条,当前时间{},花费时间 = {}", ret.get(), DateUtil.now(), (endTime - startTime));
        } else {
            log.info("答辩资格审核刷新成功0条数据,当前时间{}", DateUtil.now());
        }
    }

posted on   HeavenTang  阅读(19)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示