字面意思是倒计时类
类名其实说的很形象,倒数到0时线程解除阻塞,继续往下执行
测试类
public class TestCDL {
private static CountDownLatch countDownLatch = new CountDownLatch(3);
public static void test(){
Thread thread = new Thread(){
@Override
public void run() {
try {
//阻塞在这儿了
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
System.out.println("after await" + this.getId());
}
}
};
thread.start();
//count = 3
System.out.println("Count:" + countDownLatch.getCount());
countDownLatch.countDown();
//count = 2
System.out.println("Count:" + countDownLatch.getCount());
countDownLatch.countDown();
countDownLatch.countDown();
//count = 0 ;线程解除了阻塞
System.out.println("Count:" + countDownLatch.getCount());
}
public static void main(String[] args) {
test();
}
}
类名其实说的很形象,倒数到0时线程解除阻塞,继续往下执行
测试类
public class TestCDL {
private static CountDownLatch countDownLatch = new CountDownLatch(3);
public static void test(){
Thread thread = new Thread(){
@Override
public void run() {
try {
//阻塞在这儿了
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
System.out.println("after await" + this.getId());
}
}
};
thread.start();
//count = 3
System.out.println("Count:" + countDownLatch.getCount());
countDownLatch.countDown();
//count = 2
System.out.println("Count:" + countDownLatch.getCount());
countDownLatch.countDown();
countDownLatch.countDown();
//count = 0 ;线程解除了阻塞
System.out.println("Count:" + countDownLatch.getCount());
}
public static void main(String[] args) {
test();
}
}