3.1.6 循環柵欄:CyclicBarrier
阿新 • • 發佈:2018-02-07
main cli eat 程序 util gpo lee 柵欄 read
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");
}
}
3.1.6 循環柵欄:CyclicBarrier