import java.util.concurrent.CountDownLatch;
/**
* @date: 2022/3/19 11:09
* @version: 1.0
*/
public class CountDownLatchTest {
public static void main(String[] args) throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(5);
myCountDownlatch myCountDownlatch = new myCountDownlatch(countDownLatch);
long start = System.currentTimeMillis();
for(int i=0;i<5;i++){
new Thread(myCountDownlatch).start();
}
countDownLatch.await();
long end = System.currentTimeMillis();
System.out.println("耗时:"+(end-start));
}
public static class myCountDownlatch implements Runnable{
private CountDownLatch countDownLatch;
public myCountDownlatch(CountDownLatch countDownLatch){
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
synchronized (this){
try{
for(int i =0;i<500;i++){
if(i % 2==0){
System.out.println(i);
}
}
}finally {
countDownLatch.countDown();
}
}
}
}
}
评论 (0)