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); } }