多线程简单应用ExecutorService

线程池

        int threadSize = 5000;  //每个线程执行的数据
        int dataSize = applylist.size(); //总数据量
        //计算需要几个线程
        int threadNum = (dataSize % threadSize)>0?(dataSize / threadSize)+1:(dataSize / threadSize); 
        ExecutorService executor = Executors.newFixedThreadPool(threadNum);//启用多线程
           for(int i = 0; i < threadNum ; i++){
        	int start  = i * threadSize;
			int end = (i+1) < threadNum ? (i+1) * threadSize :  dataSize;
			//每个线程执行的业务方法
			executor.execute(new SechedulWorkRunable( applylist, start, end, tblApplyinfoSchedullist,tblApplyinfoSchedulRepository));
			
        }
        
        executor.shutdown();
        

SechedulWorkRunable

public class SechedulWorkRunable implements  Runnable{
	
	
	@Autowired
	TblApplyinfoRepository tblApplyinfoRepository;
	@Autowired
	TblApplyinfoSchedulRepository tblApplyinfoSchedulRepository;
	

	private List<TblApplyinfo> datas = null;

	private int start;

	private int end;
	
	List<TblApplyinfoSchedul> tblApplyinfoSchedullist = null;

	public SechedulWorkRunable(List<TblApplyinfo> datas ,int start, int end,List<TblApplyinfoSchedul> tblApplyinfoSchedullist,TblApplyinfoSchedulRepository tblApplyinfoSchedulRepository){
		this.datas = datas;
		this.start = start;
		this.end = end;
		this.tblApplyinfoSchedullist = tblApplyinfoSchedullist;
		this.tblApplyinfoSchedulRepository = tblApplyinfoSchedulRepository;
	}
	
	public void run() {  

	
		for(int i = start; i< end ;i ++){
			
			TblApplyinfo tblApplyinfo = datas.get(i);
			TblApplyinfoSchedul tblApplyinfoSchedul = tblApplyinfoSchedulRepository.findById(tblApplyinfo.getId());
    		if(tblApplyinfoSchedul == null){
    			TblApplyinfoSchedul tblApplyinfoSchedul2 = new TblApplyinfoSchedul();
    			tblApplyinfoSchedul2.setChildIdno(tblApplyinfo.getChildIdno());
    			tblApplyinfoSchedul2.setChildName(tblApplyinfo.getChildName());
    			tblApplyinfoSchedul2.setId(tblApplyinfo.getId());
    			tblApplyinfoSchedulRepository.save(tblApplyinfoSchedul2);
    		}
    		
    		
		}
	
		
	
	}

上一篇:线程池 -实现线程复用


下一篇:oh-my-zsh配置 alias 指定指令别名