多線程分發處理List集合數據


//List集合
final List<PlyDayList> plyVO = plyDayListDao.selectPlyDayListKey(dataSumNo,sd,tstate);
if(plyVO != null && plyVO.size() > 0){
	//創建一個線程池
	try {
		int threadNum  = 10;//線程數自定義
		int threadSize = plyVO.size()/threadNum;//給每個線程分發處理條數(總條數/線程數);
		ExecutorService eService = Executors.newFixedThreadPool(threadNum);//創建線程池
		List<Callable<String>> cList = new ArrayList<Callable<String>>(); 
		Callable<String> task = null;
		List<PlyDayList> sList = null;
		for(int i=0;i<threadNum;i++){
			if(i == threadNum - 1){
				sList = plyVO.subList(i*threadSize, plyVO.size());
			} else {
				sList = plyVO.subList(i*threadSize, (i+1)*threadSize);
			}
			final List<PlyDayList> nowList = sList;
			task = new Callable<String>() {
				@Override
				public String call() throws Exception {
					StringBuffer sb = new StringBuffer();
					for(int j=0;j<nowList.size();j++){
						//處理需要處理的業務
						int s = plyDayService.nvhlInsuranceResponse(nowList.get(j)); 
						sb.append(s+"_");
					}
					//返回處理的結果集
					return sb.toString();
				}
			};
			cList.add(task);
		}
		List<Future<String>> results;
		results = eService.invokeAll(cList);
		for(Future<String> str:results){
			//打印結果集
			log.info(str.get());
		}
		eService.shutdown();
	} catch (Exception e) {
		e.printStackTrace();
	}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM