//需要拆分的list
List listSize =...;
//多线程执行任务
//500分一组
int count = 50;
int listSize = nucleicDetailInfos.size();
//线程数
int RunSize = (listSize / count)+1;
ThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(RunSize);
CountDownLatch countDownLatch = new CountDownLatch(RunSize);
List newList = null ;
for (int i = 0; i < RunSize; i++) {
if((i+1)==RunSize){
int startIndex = (i*count);;
int endIndex = nucleicDetailInfos.size();
newList =nucleicDetailInfos.subList(startIndex,endIndex);
}else{
int startIndex = i*count;;
int endIndex = (i+1)*count;
newList =nucleicDetailInfos.subList(startIndex,endIndex);
}
ImportTask task= new ImportTask (newList,countDownLatch,mainId,checkType,nucleicNum,startDate,endDate,hType);
executor.execute(task);
countDownLatch.await(); //主线程等待所有线程完成任务
//所有线程完成任务后的一些业务
//更新主表信息
...todo
//关闭线程池
executor.shutdown();
ImportTask.java
public class ImportTask implements Runnable{
private List<NucleicDetailInfo> list;
private CountDownLatch countDownLatch;
private Long mainId;
private String checkType;
private int nucleicNum;
private Date startDate;
private Date endDate;
private Integer hType;
protected final Logger logger = LoggerFactory.getLogger(ImportTask.class);
//注意根据上下文获取
@Autowired
private ICodeColorAbnormalInfoService iCodeColorAbnormalInfoService= SpringUtils.getBean(ICodeColorAbnormalInfoService.class);
@Autowired
private NucleicDetailInfoMapper nucleicDetailInfoMapper=SpringUtils.getBean(NucleicDetailInfoMapper.class);
@Autowired
private NucleicDetailScheduleInfoMapper nucleicDetailScheduleInfoMapper=SpringUtils.getBean(NucleicDetailScheduleInfoMapper.class);
public ImportTask (List<NucleicDetailInfo> list,CountDownLatch countDownLatch,Long mainId,String checkType,int nucleicNum,Date startDate,Date endDate,Integer hType){
this.list = list ;
this.countDownLatch=countDownLatch;
this.mainId=mainId;
this.checkType=checkType;
this.nucleicNum=nucleicNum;
this.startDate=startDate;
this.endDate=endDate;
this.hType=hType;
}
@Override
public void run() {
if(null!=list){
logger.info("执行任务条数:"+list.size());
//执行的业务逻辑
extedProgram(list,mainId,checkType,nucleicNum,startDate,endDate,hType);
}
countDownLatch.countDown();//发出线程任务完成的信号
}
}