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");
}
}
posted @ 2018-02-07 10:47  anxbb  阅读(138)  评论(0编辑  收藏  举报