1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| public class Main { public static void main(String[] args) throws InterruptedException { CyclicBarrier cyc = new CyclicBarrier(2, () -> { System.out.println("到齐啦"); }); new Thread(() -> { try { cyc.await(); System.out.println("线程1运行"); } catch (InterruptedException e) { throw new RuntimeException(e); } catch (BrokenBarrierException e) { throw new RuntimeException(e); } }).start(); new Thread(() -> { try { cyc.await(); System.out.println("线程2运行"); } catch (InterruptedException e) { throw new RuntimeException(e); } catch (BrokenBarrierException e) { throw new RuntimeException(e); } }).start(); } }
|