zoukankan      html  css  js  c++  java
  • 多线程分发处理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();
    	}
    }
    
  • 相关阅读:
    K-邻近算法
    算法
    (12)ubunto 快捷键
    (38)C#IIS
    RichEditControl(富文本控件)
    Gaugecontrol(测量仪器图形控件)
    鏖战字符串
    bzoj3713 [PA2014]Iloczyn|暴力(模拟)
    约会安排HDU
    hdu4614 线段树+二分 插花
  • 原文地址:https://www.cnblogs.com/hmhhz/p/12515699.html
Copyright © 2011-2022 走看看