优快云停更,所有博客已搬至 rainofshadow.top 个人服务器。 最新文章,移至 https://rainofshadow.top/
昨天在项目中有一个excel导表的操作,于是就想着用多线程插入数据。网上找了下mabayis多线程插入数据,看了很多都是配置的xml,但我不是很喜欢写xml.....于是自己手写了一个,代码可能不是很优雅,但还是记录一下.不多说,直接上代码。
controller,和dao就不放了,
主要是service层
@Override
public int upload(Long paperId,List<InterviewProblem> interviewProblems) {
//面试卷加题
InterviewPaper interviewPaper = interviewPaperMapper.selectByPrimaryKey(paperId);
interviewPaper.setCount(interviewPaper.getCount()+interviewProblems.size());
interviewPaperMapper.updateByPrimaryKey(interviewPaper);
//写入题库
//开启一个线程池,3个线程
ExecutorService executor = Executors.newFixedThreadPool(3);
//计数器,初始为需要插入的数据集的条数
CountDownLatch countDownLatch = new CountDownLatch(interviewP