import java.util.concurrent.CountDownLatch;class Aworker implements Runnable {private int num;private CountDownLatch begin;private CountDownLatch end;public Aworker(int num, final CountDownLatch begin, final CountDownLatch end) {this.num = num;this.begin = begin;this.end = end;}@Overridepublic void run() {// TODO Auto-generated method stubtry {System.out.println(num + "th people is ready");begin.await();//准备就绪} catch (InterruptedException e) {e.printStackTrace();} finally {end.countDown();//计数器减一,到达终点System.out.println(num + "th people arrive");}}}public class Race {public static void main(String[] args) {int num = 10;CountDownLatch begin = new CountDownLatch(1);CountDownLatch end = new CountDownLatch(num);for (int i = 1; i <= num; i++) {new Thread(new Aworker(i, begin, end)).start();}try {Thread.sleep((long) (Math.random() * 5000));} catch (InterruptedException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}System.out.println("judge say : run !");begin.countDown(); //开始跑long startTime = System.nanoTime();try {end.await(); //等待结束} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {long endTime = System.nanoTime();System.out.println("judge say : all arrived !");System.out.println("spend time: " + (endTime - startTime));}}}
原文地址:http://blog.csdn.net/csujiangyu/article/details/44236205