import java.util.concurrent.CountDownLatch;
public class TestCountDownLatch {
public static void main(String[] args) throws InterruptedException {
final CountDownLatch c = new CountDownLatch(2);
Thread th = new Thread(new Runnable(){
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+" hello , i am so happy");
c.countDown();
System.out.println(Thread.currentThread().getName()+" hello , i am soo happy");
c.countDown();
}
}, "firstThread");
th.start();
c.await();
System.out.println("this is it");
}
}
countDownLatch 的countDown的方法时, N就减一,await 方法会阻塞当前线程, 直到N等于0;