栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

list拆分任务多线程执行

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

list拆分任务多线程执行

            //需要拆分的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 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 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();//发出线程任务完成的信号
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1014295.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号