Java
背景 这两JUC类适合放一起比较,随便写写,简单小结。 CountDownLatch 要点: 底层采用AQS队列,源码很简单300行。 使用: 初始化对象并填入(计数器)值,例如:new CountDownLatch(3)。多线程执行完毕时,触发countdown方法将计数器值减1,当值为0时,主线程中被Latch拴住的代码开始执行。 用法一: public class CountDownLatch_v1 { //计数器设为3 static CountDownLatch latch = new CountDownLatch ( 3 ) ; public static void main ( String [ ] args ) throws InterruptedException { Thread t1 = new Thread ( ( ) - > { latch . countDown ( ) ; System . out . println ( "T1 is OK !" ) ; } ) ; Thread t2 = new Thread ( ( ) - > { System . out . println ( "T2 is OK !" ) ; latch . countDown ( ) ; } ) ; Thread t3 = new Thread ( ( ) - > {