多线程异步查询

 

 

 

 

private ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(20,
new BasicThreadFactory.Builder().namingPattern("xxxxxxx-schedule-pool-%d").daemon(true).build());

ExecutorService detailExecutorService = Executors.newFixedThreadPool(500);
        protected static final ExecutorService executorService = Executors.newScheduledThreadPool(2 * 2 + 1, Executors.defaultThreadFactory());
            
        CountDownLatch latch = new CountDownLatch(3);
        

        executorService.submit(() -> {
            try {
            
                method 1 ...
                
            } finally {
                latch.countDown();
            }
        });


        executorService.submit(() -> {
            try {
            
                method 2 ...
                
            } finally {
                latch.countDown();
            }
        });
        
        executorService.submit(() -> {
            try {
            
                method 3 ...
                
            } finally {
                latch.countDown();
            }
        });

        try {
            latch.await(5L, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            logger.error("InterruptedException happened=", e);
        }

 

 

 

 

      protected static final ExecutorService timeLineFixExecutorService = Executors.newScheduledThreadPool(500, Executors.defaultThreadFactory());

      List<ContactsTimeLine> list = contactsTimeLineService.getLatestTimeLine(contactsTimeLineQuery);
      if(list!=null&&list.size()>0){
                int size=list.size();
                CountDownLatch latch = new CountDownLatch(size);
                for (ContactsTimeLine timeLine : list) {
                    ContactsPerson person = null;
                    if(contactsPersonMap.containsKey(timeLine.getContactId())){
                        person = contactsPersonMap.get(timeLine.getContactId());
                    }
                    if (person != null) {
                        timeLineFixExecutorService.submit(()-> setTimeLineData(timeLine,latch));  //注意setTimeLineData方法最后finally做latch.countDown()
                    }
                }
                try {
                    latch.await(10, TimeUnit.SECONDS);
                } catch (InterruptedException e) {
                    logger.error("联系人动态查询,timeout: {}", e);
                }
            }

 

posted @ 2018-07-25 20:27  wanhua.wu  阅读(365)  评论(0编辑  收藏  举报