/**
* 闭锁:CountDownLatch在完成某些运算时,只有其他所有线程的运算全部完成,当前运算才继续执行
* @author lijinping
*/
public class TestCountDownLatch {
public static void main(String[] args) {
final CountDownLatch latch = new CountDownLatch(5);
long start = System.currentTimeMillis();
Runnable r = () -> {
for(int i=0;i<5000;i++) {
if(i%2==0) {
System.out.println(i);
}
}
latch.countDown();
};
for(int i=0;i<5;i++) {
new Thread(r).start();
}
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("耗费时间为:"+(end-start));
}
}
CountDownLatch
最新推荐文章于 2020-06-07 00:42:11 发布