1 package com.noway.test.concurrent.cyclicBarrier;
2
3 import java.util.concurrent.BrokenBarrierException;
4 import java.util.concurrent.CyclicBarrier;
5
6 /**
7 * 多线程中如何'同时'启动多个线程
8 * @author Noway
9 *
10 */
11 public class TestCyclicBarrier {
12 public static void main(String[] args) {
13 CyclicBarrier cb = new CyclicBarrier(3);
14 new Thread(new cbThread(cb,"张三")).start();
15 new Thread(new cbThread(cb,"李四")).start();
16 new Thread(new cbThread(cb,"王五")).start();
17 new Thread(new cbThread(cb,"马六")).start();
18 new Thread(new cbThread(cb,"小七")).start();
19 }
20 }
21
22 class cbThread implements Runnable{
23 private CyclicBarrier cb;
24 private String name;
25 public cbThread(CyclicBarrier cb, String name) {
26 super();
27 this.cb = cb;
28 this.name = name;
29 }
30 @Override
31 public void run() {
32 System.out.println(this.name+"准备好了...");
33 try {
34 this.cb.await();//形成一个屏障
35 } catch (InterruptedException e) {
36 e.printStackTrace();
37 } catch (BrokenBarrierException e) {
38 e.printStackTrace();
39 }
40 System.out.println(this.name+"出发了...");
41 }
42
43 }