本章主要对CountDownLatch的基本方法进行学习。
有关CountDownLatch的应用实例详见下一章:《Java并发:31》
CountDownLatch,是JDK1.5的java.util.concurrent并发包中提供的一个并发工具类。
所谓CountDown即 倒计时 的意思,所谓Latch即 门闩 的意思。
所以综合起来,CountDownLatch指的就是 倒计时门闩,虽然这个叫法很奇怪,但是确能很好地表示它的作用。
其作用在JDK注释中是这样描述的:
A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.
简单来说,CountDownLatch就是一种同步辅助工具类,这个工具类实现的效果是:
让一个或多个线程持续等待,直到其他多线程执行的一组操作全部完成以后,这些等待的线程才会继续执行。
下面简单介绍一些CountDownLatch的实现原理。
引用[July 18, 2013 by Lokesh Gupta]的一段话和图:
CountDownLatch是通过一个计数器来实现的,计数器的初始值为线程的数量。
每当一个线程完成了自己的任务后,计数器的值就会减1。
当计数器值到达0时,它表示所有的线程已经完成了任务,然后在等待的线程就可以恢复执行任务。
CountDownLatch的主要方法很简单,如下:
注意:
场景说明:
实例代码:
//await()/await(timeout,TimeUnit) 与countDown() /** * 线程 等待方法 终止原因 * ------------------------------------------------ * thread-0 await() count=0 * thread-1 await() interrupted * ------------------------------------------------ * thread-2 await(timeout,TimeUnit) timeout * thread-3 await(timeout,TimeUnit) interrupted * thread-4 await(timeout,TimeUnit) count=0 */ System.out.println(); CountDownLatch latch = new CountDownLatch(num); //await() 直至latch 的count = 0 new Thread(() -> { LOGGER.info(Thread.currentThread().getName() + "(await0) is awaiting...."); try { latch.await(); LOGGER.info(Thread.currentThread().getName() + "(await0) is terminated because the latch‘s count is zero."); } catch (InterruptedException e) { LOGGER.info(Thread.currentThread().getName() + "(await0) is interrupted."); //e.printStackTrace(); } }).start(); //await() 被中断 Thread thread = new Thread(() -> { LOGGER.info(Thread.currentThread().getName() + "(await1) is awaiting...."); try { latch.await(); LOGGER.info(Thread.currentThread().getName() + "(await1) is terminated because the latch‘s count is zero."); } catch (InterruptedException e) { LOGGER.info(Thread.currentThread().getName() + "(await1) is terminated because it is interrupted."); //e.printStackTrace(); } }); thread.start(); //await(timeout,TimeUnit) //await(timeout,TimeUnit) timeout new Thread(() -> { LOGGER.info(Thread.currentThread().getName() + "(await2) is awaiting...."); try { //等待2秒 boolean result = latch.await(1, TimeUnit.SECONDS); //如果等到了count=0,则返回true,并停止等待 if (result) {//如果返回结果为true,表示在等待时间耗尽之前等到了count=0 LOGGER.info(Thread.currentThread().getName() + "(await2) is terminated because the latch‘s count is zero."); } else {//如果返回结果为false,表示等待时间耗尽,也没有等到count=0 LOGGER.info(Thread.currentThread().getName() + "(await2) is terminated because the the waiting time is timeout."); } } catch (InterruptedException e) { LOGGER.info(Thread.currentThread().getName() + "(await2) is terminated because it is interrupted."); //e.printStackTrace(); } }).start(); //await(timeout,TimeUnit) Thread thread1 = new Thread(() -> { LOGGER.info(Thread.currentThread().getName() + "(await3) is awaiting...."); try { //等待3秒 boolean result = latch.await(5, TimeUnit.SECONDS); //如果等到了count=0,则返回true,并停止等待 if (result) {//如果返回结果为true,表示在等待时间耗尽之前等到了count=0 LOGGER.info(Thread.currentThread().getName() + "(await3) is terminated because the latch‘s count is zero."); } else {//如果返回结果为false,表示等待时间耗尽,也没有等到count=0 LOGGER.info(Thread.currentThread().getName() + "(await3) is terminated because the the waiting time is timeout."); } } catch (InterruptedException e) { LOGGER.info(Thread.currentThread().getName() + "(await3) is terminated because it is interrupted."); //e.printStackTrace(); } }); thread1.start(); //await(timeout,TimeUnit) count=0 new Thread(() -> { LOGGER.info(Thread.currentThread().getName() + "(await4) is awaiting...."); try { //等待3秒 boolean result = latch.await(5, TimeUnit.SECONDS); //如果等到了count=0,则返回true,并停止等待 if (result) {//如果返回结果为true,表示在等待时间耗尽之前等到了count=0 LOGGER.info(Thread.currentThread().getName() + "(await4) is terminated because the latch‘s count is zero."); } else {//如果返回结果为false,表示等待时间耗尽,也没有等到count=0 LOGGER.info(Thread.currentThread().getName() + "(await4) is terminated because the the waiting time is timeout."); } } catch (InterruptedException e) { LOGGER.info(Thread.currentThread().getName() + "(await4) is interrupted."); //e.printStackTrace(); } }).start(); Thread.sleep(500); System.out.println(); //等待4秒 Thread.sleep(2000); System.out.println(); //中断 thread.interrupt(); thread1.interrupt(); Thread.sleep(1000); System.out.println(); //进行自减,直到count=0 while (latch.getCount() > 0) { //LOGGER.info(Thread.currentThread().getName() + " latch.countDown , count = " + latch.getCount()); latch.countDown(); }
运行结果:
2018-03-31 22:08:56 INFO CountDownLatchBasic:56 - Thread-0(await0) is awaiting.... 2018-03-31 22:08:56 INFO CountDownLatchBasic:117 - Thread-4(await4) is awaiting.... 2018-03-31 22:08:56 INFO CountDownLatchBasic:99 - Thread-3(await3) is awaiting.... 2018-03-31 22:08:56 INFO CountDownLatchBasic:81 - Thread-2(await2) is awaiting.... 2018-03-31 22:08:56 INFO CountDownLatchBasic:67 - Thread-1(await1) is awaiting.... 2018-03-31 22:08:57 INFO CountDownLatchBasic:89 - Thread-2(await2) is terminated because the the waiting time is timeout. 2018-03-31 22:08:58 INFO CountDownLatchBasic:110 - Thread-3(await3) is terminated because it is interrupted. 2018-03-31 22:08:58 INFO CountDownLatchBasic:72 - Thread-1(await1) is terminated because it is interrupted. 2018-03-31 22:08:59 INFO CountDownLatchBasic:59 - Thread-0(await0) is terminated because the latch‘s count is zero. 2018-03-31 22:08:59 INFO CountDownLatchBasic:123 - Thread-4(await4) is terminated because the latch‘s count is zero.
通过分析执行结果,发现每个线程的终止方式都如预料的那样。
对CountDownLatch的重点方法进行总结:
Java并发30:CountDownLatch(上)--基本方法学习
原文:https://www.cnblogs.com/yaochunhui/p/13489221.html