File tree Expand file tree Collapse file tree
Multithread/src/com/juc/aqs Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /**
2+ * @program JavaBooks
3+ * @description: CyclicBarrierDemo
4+ * @author: mf
5+ * @create: 2020/02/15 17:21
6+ */
7+
8+ package com .juc .aqs ;
9+
10+ import java .util .concurrent .BrokenBarrierException ;
11+ import java .util .concurrent .CyclicBarrier ;
12+
13+ public class CyclicBarrierDemo {
14+ public static void main (String [] args ) {
15+ cyclicBarrierTest ();
16+ }
17+
18+ public static void cyclicBarrierTest () {
19+ CyclicBarrier cyclicBarrier = new CyclicBarrier (7 , () -> {
20+ System .out .println ("====召唤神龙====" );
21+ });
22+ for (int i = 0 ; i < 7 ; i ++) {
23+ final int tempInt = i ;
24+ new Thread (() -> {
25+ System .out .println (Thread .currentThread ().getName () + " 收集到第" + tempInt + "颗龙珠" );
26+ try {
27+ cyclicBarrier .await ();
28+ } catch (InterruptedException e ) {
29+ e .printStackTrace ();
30+ } catch (BrokenBarrierException e ) {
31+ e .printStackTrace ();
32+ }
33+ }, "" + i ).start ();
34+ }
35+ }
36+ }
You can’t perform that action at this time.
0 commit comments