1700451717
1700451718
//参加赛跑人数
1700451719
1700451720
int num=10;
1700451721
1700451722
//发令枪只响一次
1700451723
1700451724
CountDownLatch begin=new CountDownLatch(1);
1700451725
1700451726
//参与跑步有多个
1700451727
1700451728
CountDownLatch end=new CountDownLatch(num);
1700451729
1700451730
//每个跑步者一个跑道
1700451731
1700451732
ExecutorService es=Executors.newFixedThreadPool(num);
1700451733
1700451734
//记录比赛成绩
1700451735
1700451736
List<Future<Integer>>futures=new ArrayList<Future<Integer>>();
1700451737
1700451738
//跑步者就位,所有线程处于等待状态
1700451739
1700451740
for(int i=0;i<num;i++){
1700451741
1700451742
futures.add(es.submit(new Runner(begin, end)));
1700451743
1700451744
}
1700451745
1700451746
//发令枪响,跑步者开始跑步
1700451747
1700451748
begin.countDown();
1700451749
1700451750
//等待所有跑步者跑完全程
1700451751
1700451752
end.await();
1700451753
1700451754
int count=0;
1700451755
1700451756
//统计总分
1700451757
1700451758
for(Future<Integer>f:futures){
1700451759
1700451760
count+=f.get();
1700451761
1700451762
}
1700451763
1700451764
System.out.println(“平均分数为:”+count/num);
1700451765
1700451766
}
[
上一页 ]
[ :1.700451717e+09 ]
[
下一页 ]