???????о??????????????????????????????в??????????????????????????????????????????????????
?????????java.util.concurrent.CyclicBarrier ?????
????CyclicBarrier???????????????????????????????????????????????????? (common barrier point)?????漰???????С??????????У???Щ??????????????????? CyclicBarrier ????????????????????????????????await()?????????????????????????????????????????await()??????
????int index = --count;
????if (index == 0) {  // tripped
????//????
????}
????else {
????// We're about to finish waiting even if we had not
????// been interrupted?? so this interrupt is deemed to
????// "belong" to subsequent execution.
????Thread.currentThread().interrupt();
????}
????????????????????????????????м????0???????????????ж??
????????????????????????????????await()???????????????μ????
????????????
????CyclicBarrier(int parties)
????????????μ? CyclicBarrier?????????????????????????????????????????????????????? barrier ????????????????
????CyclicBarrier(int parties?? Runnable barrierAction)
????????????μ? CyclicBarrier???????????????????????????????????????????????? barrier ???и???????????????ò????????????? barrier ????????
???????????????????Runnable?????ε??﹫??????????????????????????Runnable?????????д???????Runnable
/**
* CyclicBarrier??????CountDownLatch????????????
* ???????CyclicBarrier???????????CyclicBarrier.await()???????????????
* ???????????CyclicBarrier?????漲???????????н????????????????????????
* CyclicBarrier?????????????????????????????? ???е???????????????????????????
* CyclicBarrier????????????Runnable???????
* ??Runnable??????CyclicBarrier??????????????????????????????С?
*/
public class CyclicBarrierTest {
public static void main(String[] args) {
ExecutorService service = Executors.newCachedThreadPool();
//final  CyclicBarrier cb = new CyclicBarrier(3);//????CyclicBarrier????????3???????????
final  CyclicBarrier cb = new CyclicBarrier(3??new Runnable(){
@Override
public void run() {
System.out.println("********???????***********");
}
});
for(int i=0;i<3;i++){
Runnable runnable = new Runnable(){
public void run(){
try {
Thread.sleep((long)(Math.random()*10000));
System.out.println("???" + Thread.currentThread().getName() +
"???????O????1?????????" + cb.getNumberWaiting() + "???????????????");
cb.await();//?????????д????????????????????????????????????????????д???????????????????????
Thread.sleep((long)(Math.random()*10000));
System.out.println("???" + Thread.currentThread().getName() +
"???????O????2?????????" + cb.getNumberWaiting() + "???????????????");
cb.await();    //????CyclicBarrier?????????????
Thread.sleep((long)(Math.random()*10000));
System.out.println("???" + Thread.currentThread().getName() +
"???????O????3?????????" + cb.getNumberWaiting() + "???????????????");
cb.await();
} catch (Exception e) {
e.printStackTrace();
}
}
};
service.execute(runnable);
}
service.shutdown();
}
}