HeavenTang

导航

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 2022-10-19 15:25  HeavenTang  阅读(24)  评论(0)    收藏  举报