java 多线程应用实例

2017-10-09  本文已影响0人  haiyong6
//多线程查询
//定义异步计数器   管理一共多少个线程 
CountDownLatch latch = new CountDownLatch(dates.length); 
 //接受返回的结果 
Map resultListMap = new HashMap();
//线程池管理类(核心线程数,最大线程数,超过核心线程数的空闲线程最大存活时间,时间单位,阻塞任务队列) 
ThreadPoolExecutor exce = new ThreadPoolExecutor(200, 150000, 1,TimeUnit.NANOSECONDS,new SynchronousQueue<Runnable>());

//for循环多条异步查询
for(int i = 0; i < dates.length; i++){
    //设置参数
    Map paramNew = new HashMap();
    String beginDate = dates[i].split(",")[0]; //开始时间
    String endDate = dates[i].split(",")[1];   //结束时间
    paramsMap.put("beginDate", beginDate.replace("-", ""));
    paramsMap.put("endDate", endDate.replace("-", ""));
    paramsMap.put(objIdKey, objId);
    if(!"0".equals(bodyTypeId)) {
       paramsMap.put("bodyTypeId", bodyTypeId);
     }
    paramNew.putAll(paramsMap);
    paramNew.put("selectType", "query");
    try {
    exce.execute(new Worker(latch, paramNew, resultListMap, dao, i)); 
    } catch (Exception e) {
    e.printStackTrace();
    }
}

exce.shutdown();
try {
 //线程阻塞,等待所有线程完成才继续执行下面的代码 
    latch.await();
} catch (InterruptedException e) {
    e.printStackTrace();
}
System.out.println("=================over================");

//拿到查询结果挨个做处理
for(int i = 0; i < dates.length; i++){
      //参数设置
      String beginDate = dates[i].split(",")[0]; //开始时间
      String endDate = dates[i].split(",")[1];   //结束时间
      String legend = beginDate.substring(0, 4); //图例
      paramsMap.put("beginDate", beginDate.replace("-", ""));
      paramsMap.put("endDate", endDate.replace("-", ""));
      paramsMap.put(objIdKey, objId);
      if(!"0".equals(bodyTypeId)) {
        paramsMap.put("bodyTypeId", bodyTypeId);
       }
      //把查询结果按顺序拿出来
      List<PriceIndexEntity> list = new ArrayList<PriceIndexEntity>();
      //成交价维度新算法
      list = (List<PriceIndexEntity>) resultListMap.get(i);
      //对list处理
      ......
}

/***内部类Worker***/
static public class Worker extends Thread{

         //定义异步计数器   管理一共多少个线程 
        private CountDownLatch latch; 
        //参数 
        private Map param; 
        //要返回的结果集 
        private Map resultListMap;
        //重新给一个dao
        private IPriceIndexDao dao;
        //排序字段
        private int i;
         
        public Worker(CountDownLatch latch, Map param, Map resultListMap, IPriceIndexDao dao, int i) { 
           super(); 
           this.latch = latch; 
           this.param = param; 
           this.resultListMap = resultListMap;
           this.dao = dao;
           this.i = i;
        } 

        @Override 
        public void run() { 
            try {
                List list = null;
                if("export".equals(param.get("selectType"))){
                    if(param.get("priceType").equals("1") ){
                        list = dao.exportPriceIndexOriginalTpData(param);
                    } else{
                        list = dao.exportPriceIndexOriginalMsrpData(param);
                    }
                } else{
                    if(param.get("priceType").equals("1") ){
                        list = dao.getPriceIndexAnalysTpData(param);
                    } else{
                        list = dao.getPriceIndexAnalysMsrpData(param);
                    }
                }
                resultListMap.put(i, list);
                   //当前线程执行完  计数器-1 
                latch.countDown(); 
            } catch (Exception e) {
                e.printStackTrace();
            }
        
        } 

    }
上一篇 下一篇

猜你喜欢

热点阅读