CountDownLatch和ExecutorService 线程池cachedThreadPool.submit
CountDownLatch运用
CountDownLatch和ExecutorService 线程池cachedThreadPool.submit
1、CountDownLatch 概念
CountDownLatch可以使一个获多个线程等待其他线程各自执行完毕后再执行。
CountDownLatch 定义了一个计数器,和一个阻塞队列, 当计数器的值递减为0之前,阻塞队列里面的线程处于挂起状态,当计数器递减到0时会唤醒阻塞队列所有线程,这里的计数器是一个标志,可以表示一个任务一个线程,也可以表示一个倒计时器,
CountDownLatch可以解决那些一个或者多个线程在执行之前必须依赖于某些必要的前提业务先执行的场景。
点击查看代码
public void studentListInit(StartPlanMajorDto startPlanMajorDto) {
// TODO:2023-2-27 只加在读和复学的
List<StudentStatusVo> studentStatusVos = startPlanMajorMapper.selectStudentList(startPlanMajorDto);
List<String> classIdList = startPlanMajorMapper.selectClassIdList(startPlanMajorDto);
List<TeachingClassVo> teachingClassVos = startPlanMajorMapper.selectTeachingClassBySemesterId(startPlanMajorDto.getSemesterId());
ExecutorService cachedThreadPool = Executors.newFixedThreadPool(classIdList.size());
CountDownLatch latch = new CountDownLatch(classIdList.size());
// 遍历教学班中的行政班,将对应行政班的学生添加到选课名单表中
for(String classId : classIdList){
cachedThreadPool.submit(() -> {
List<CourseSelectionListEntity> courseSelectionListEntities = new ArrayList<>();
List<TeachingClassVo> filterList = teachingClassVos.stream().filter(item -> item.getManagementClassId().contains(classId)).collect(Collectors.toList());
for(TeachingClassVo teachingClassVo : filterList){
// 找出这个行政班级中的学生ID数组
List<String> studentIdList = studentStatusVos.stream().filter(item -> item.getStudentClass().equals(classId))
.map(item -> item.getStudentId()).collect(Collectors.toList());
for(String studentId : studentIdList){
CourseSelectionListEntity courseSelectionListEntity = new CourseSelectionListEntity();
courseSelectionListEntity.setCourseSelectionListId(uuIDStringGenerator.nextUUID());
courseSelectionListEntity.setPreSetSign("1");
courseSelectionListEntity.setSemesterId(teachingClassVo.getSemesterId());
courseSelectionListEntity.setCourseId(teachingClassVo.getCourseId());
courseSelectionListEntity.setOpenWay(teachingClassVo.getStartPlanMajorOpenSign());
courseSelectionListEntity.setTeachingClassId(teachingClassVo.getTeachingClassId());
courseSelectionListEntity.setTeachingClassTheoryId(teachingClassVo.getTeachingClassTheoryId());
courseSelectionListEntity.setTeachingClassExperimentId(teachingClassVo.getTeachingClassExperimentId());
courseSelectionListEntity.setTeachingClassPracticeId(teachingClassVo.getTeachingClassPracticeId());
courseSelectionListEntity.setStudentId(studentId);
// courseSelectionListEntity.setCreator(AuthorityUtil.getLoginUser().getUserId());
// 该学生如果已经在教学班中了,则不重复添加
int count = startPlanMajorMapper.existsCourseSelection(studentId,teachingClassVo.getTeachingClassId());
if(count <= 0){
courseSelectionListEntities.add(courseSelectionListEntity);
}
}
}
if(courseSelectionListEntities.size() > 0){
startPlanMajorMapper.batchInsert(courseSelectionListEntities);
}
latch.countDown();
});
}
cachedThreadPool.shutdown();
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
posted on 2023-02-27 10:30 HeavenTang 阅读(161) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix