多线程分发处理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();
	}
}

 

posted @ 2020-03-18 10:11  超神之巅  阅读(1978)  评论(0编辑  收藏  举报