1.应用场景
CyclicBarrier应用于多个线程必须同时开始
2.代码
private static CyclicBarrier cyclicBarrier = new CyclicBarrier(5); public static void main(String[] args) { ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < 5; i++) { final int index = i; try{ Thread.sleep(1000); }catch (InterruptedException e){ e.printStackTrace(); } executorService.execute(new Runnable() { @Override public void run() { go(); } }); } executorService.shutdown(); } private static void go() { System.out.println(Thread.currentThread().getName()+"准备就绪"); try { cyclicBarrier.await(); System.out.println(Thread.currentThread().getName()+"开始执行"); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } }
标签:Thread,void,printStackTrace,屏障,static,catch,CyclicBarrier,循环 From: https://www.cnblogs.com/blanset/p/16785437.html