3.1.6 循环栅栏:CyclicBarrier

package 第三章.循环栅栏CyclicBarrier;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
* Created by zzq on 2018/1/24.
*/
public class CyclicBarrierTeST implements Runnable{
static CyclicBarrier cyclicBarrier=new CyclicBarrier(2);
public void run() {
System.out.println(Thread.currentThread().getName()+"准备开始");
try {
Thread.sleep(2000);
cyclicBarrier.await();
System.out.println(Thread.currentThread().getName()+"开始工作-------");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}

public static void main(String[] args) throws BrokenBarrierException, InterruptedException {
CyclicBarrierTeST cyclicBarrierTeST=new CyclicBarrierTeST();
System.out.println("开始执行程序");
Thread thread=new Thread(cyclicBarrierTeST);
Thread thread2=new Thread(cyclicBarrierTeST);
thread.start();
thread2.start();
thread.join();
thread2.join();
System.out.println("fdsafsadfsadas");
}
}
原文地址:https://www.cnblogs.com/anxbb/p/8425548.html