首页 > 代码库 > CyclicBarrier
CyclicBarrier
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();
}
}
}
CyclicBarrier