java并发包CountDownLatch
CountDownLatch类位于java.util.concurrent包下,利用它可以实现类似计数器的功能。 这里举个例子: 比如有一个任务A,它要等待其他4个任务执行完毕之后才能执行,此时就可以利用CountDownLatch来实现这种功能了。 代码如下: package com . newDemo . controller . test ; import java . util . concurrent . CountDownLatch ; public class threadDemo21 { public static void main ( String [ ] args ) throws InterruptedException { System . out . println ( "等待子线程执行完毕..." ) ; final CountDownLatch countDownLatch = new CountDownLatch ( 2 ) ; Thread t1 = new Thread ( new Runnable ( ) { public void run ( ) { System . out . println ( "子线程," + Thread . currentThread ( ) . getName ( ) + "开始执行..." ) ;