CountDownLatch和ExecutorService 线程池cachedThreadPool.submit

[复制链接]
发表于 2023-2-27 11:11:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
CountDownLatch运用
CountDownLatch和ExecutorService 线程池cachedThreadPool.submit
1、CountDownLatch 概念
CountDownLatch可以使一个获多个线程等待其他线程各自执行完毕后再执行。
CountDownLatch 定义了一个计数器,和一个阻塞队列, 当计数器的值递减为0之前,阻塞队列里面的线程处于挂起状态,当计数器递减到0时会唤醒阻塞队列所有线程,这里的计数器是一个标志,可以表示一个任务一个线程,也可以表示一个倒计时器,
CountDownLatch可以解决那些一个或者多个线程在执行之前必须依赖于某些必要的前提业务先执行的场景。
点击查看代码
  1.   public void studentListInit(StartPlanMajorDto startPlanMajorDto) {
  2.             // TODO:2023-2-27 只加在读和复学的
  3.         List<StudentStatusVo> studentStatusVos = startPlanMajorMapper.selectStudentList(startPlanMajorDto);
  4.         List<String> classIdList = startPlanMajorMapper.selectClassIdList(startPlanMajorDto);
  5.         List<TeachingClassVo> teachingClassVos = startPlanMajorMapper.selectTeachingClassBySemesterId(startPlanMajorDto.getSemesterId());
  6.         ExecutorService cachedThreadPool = Executors.newFixedThreadPool(classIdList.size());
  7.         CountDownLatch latch = new CountDownLatch(classIdList.size());
  8.         // 遍历教学班中的行政班,将对应行政班的学生添加到选课名单表中
  9.         for(String classId : classIdList){
  10.             cachedThreadPool.submit(() -> {
  11.                 List<CourseSelectionListEntity> courseSelectionListEntities = new ArrayList<>();
  12.                 List<TeachingClassVo> filterList = teachingClassVos.stream().filter(item -> item.getManagementClassId().contains(classId)).collect(Collectors.toList());
  13.                 for(TeachingClassVo teachingClassVo : filterList){
  14.                     // 找出这个行政班级中的学生ID数组
  15.                     List<String> studentIdList = studentStatusVos.stream().filter(item -> item.getStudentClass().equals(classId))
  16.                             .map(item -> item.getStudentId()).collect(Collectors.toList());
  17.                     for(String studentId : studentIdList){
  18.                         CourseSelectionListEntity courseSelectionListEntity = new CourseSelectionListEntity();
  19.                         courseSelectionListEntity.setCourseSelectionListId(uuIDStringGenerator.nextUUID());
  20.                         courseSelectionListEntity.setPreSetSign("1");
  21.                         courseSelectionListEntity.setSemesterId(teachingClassVo.getSemesterId());
  22.                         courseSelectionListEntity.setCourseId(teachingClassVo.getCourseId());
  23.                         courseSelectionListEntity.setOpenWay(teachingClassVo.getStartPlanMajorOpenSign());
  24.                         courseSelectionListEntity.setTeachingClassId(teachingClassVo.getTeachingClassId());
  25.                         courseSelectionListEntity.setTeachingClassTheoryId(teachingClassVo.getTeachingClassTheoryId());
  26.                         courseSelectionListEntity.setTeachingClassExperimentId(teachingClassVo.getTeachingClassExperimentId());
  27.                         courseSelectionListEntity.setTeachingClassPracticeId(teachingClassVo.getTeachingClassPracticeId());
  28.                         courseSelectionListEntity.setStudentId(studentId);
  29. //                            courseSelectionListEntity.setCreator(AuthorityUtil.getLoginUser().getUserId());
  30.                         // 该学生如果已经在教学班中了,则不重复添加
  31.                         int count = startPlanMajorMapper.existsCourseSelection(studentId,teachingClassVo.getTeachingClassId());
  32.                         if(count <= 0){
  33.                             courseSelectionListEntities.add(courseSelectionListEntity);
  34.                         }
  35.                     }
  36.                 }
  37.                 if(courseSelectionListEntities.size() > 0){
  38.                     startPlanMajorMapper.batchInsert(courseSelectionListEntities);
  39.                 }
  40.                 latch.countDown();
  41.             });
  42.         }
  43.         cachedThreadPool.shutdown();
  44.         try {
  45.             latch.await();
  46.         } catch (InterruptedException e) {
  47.             e.printStackTrace();
  48.         }
  49.     }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
继续阅读请点击广告
回复

使用道具 举报

×
登录参与点评抽奖,加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表