/**
* 调用方法 countDown(),会将初始化的值减少一次;
* 调用方法 await(),线程会在此等待,直到计时器为0,即被唤醒
*/
public static void main(String[] args) { final ThreadPoolExecutor countDownLatchExecutor = new ThreadPoolExecutor( 1, 10, 0, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new ThreadFactoryBuilder() .setNameFormat("CountDownLatch-Executor-%d") .setDaemon(true) .build() ); CountDownLatch latch = new CountDownLatch(T_COUNT); for (int i = 0; i < T_COUNT; i++) { Runnable task = new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName() + "执行完毕,等待其他线程执行"); latch.countDown(); } }; countDownLatchExecutor.submit(task); } try { latch.await(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " 任务结束,主线程执行"); countDownLatchExecutor.shutdown(); }