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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodeMonkey陈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值