码迷,mamicode.com
首页 > 其他好文 > 详细

CountDownLatch

时间:2018-09-12 18:06:06      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:row   finally   倒数   dom   rup   inter   .exe   size   ice   

 

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestCountDownLatch {

public static void main(String[] args) throws InterruptedException {
long start = System.currentTimeMillis();
// 开始的倒数锁
final CountDownLatch begin = new CountDownLatch(1);
// 结束的倒数锁
final CountDownLatch end = new CountDownLatch(10000);
List<String> list=new ArrayList<String>();
// 十名选手
final ExecutorService exec = Executors.newFixedThreadPool(10);
for (int index = 0; index < 10000; index++) {
final int NO = index;
Runnable run = new Runnable() {
public void run() {
try {
begin.await();
System.out.println(NO);
String temp="AA"+NO;
list.add(temp);
Thread.sleep((long) (Math.random() * 10));
} catch (InterruptedException e) {
} finally {
end.countDown();
}
}
};
System.out.println(run);
exec.submit(run);
}
System.out.println("Game Start");
begin.countDown();
end.await();
System.out.println("Game Over");
exec.shutdown();
System.out.println("num:"+list.size());
// for(String s:list){
// System.out.println("数据打印:"+s);
// }
System.out.print("共计用时 ");
System.out.println(System.currentTimeMillis() - start);
}
}

 

CountDownLatch

标签:row   finally   倒数   dom   rup   inter   .exe   size   ice   

原文地址:https://www.cnblogs.com/justuntil/p/9636087.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!