package com.alibaba.yuanmeng._concurrent;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class CyclicBarrierTest {
private static final int THREAD_COUNT = 10;
public static CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT);
public static void main(String[] args) {
for(int i = 0; i<THREAD_COUNT; i++){
new Thread(new WorkThread(barrier)).start();
}
}
}
class WorkThread implements Runnable{
private CyclicBarrier barrier;
public WorkThread(CyclicBarrier barrier){
this.barrier = barrier;
}
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + "working before await()");
barrier.await();
System.out.println(Thread.currentThread().getName() + "working after await()");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
}
原文:http://www.cnblogs.com/chenmo-xpw/p/3938454.html