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();
    	}
    }
    
  • 相关阅读:
    [最新]制作u盘引导安装ubuntu11.04
    js记录
    下面的代码有什么不妥之处
    Oracle常用命令
    蓝天下,献给你,html5
    无意义的小东西
    sql中,把varchar类型转换为int型,然后进行排序
    身边的人,来来去去
    不一定能写出来的求素数问题
    写在第一百篇博客之际
  • 原文地址:https://www.cnblogs.com/hmhhz/p/12515699.html
Copyright © 2011-2022 走看看