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
29
30
31
|
public static void main(String[] args) {
CyclicBarrier cb = new CyclicBarrier(2,()->{
System.out.println("任务执行完成,触发回调函数");
});
for (int i=0;i<2;++i) {
int finalI = i;
new Thread(()-> {
try {
cb.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
System.out.println("继续向下执行"+ finalI);
}).start();
int finalI1 = i;
new Thread(()->{
try {
cb.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
System.out.println("继续向下执行"+ finalI1);
}).start();
}
}
|