-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCyclicBarrierTest.java
More file actions
48 lines (43 loc) · 1.48 KB
/
Copy pathCyclicBarrierTest.java
File metadata and controls
48 lines (43 loc) · 1.48 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.algorithm.demo.thread;
import java.util.concurrent.CyclicBarrier;
/**
* CyclicBrrier:N个线程相互等待,任何一个线程完成之前,所有的线程都必须等待。并且所有线程都被释放后,还能通过reset来重用。
*/
public class CyclicBarrierTest {
public static void main(String[] args) {
CyclicBarrier cyclicBarrier = new CyclicBarrier(5, new Runnable() {
@Override
public void run() {
System.out.println("线程组执行结束");
}
});
for (int i = 0; i < 5; i++) {
new Thread(new readNum(i,cyclicBarrier)).start();
}
//CyclicBarrier 可以重复利用,
// 这个是CountDownLatch做不到的
// for (int i = 11; i < 16; i++) {
// new Thread(new readNum(i,cyclicBarrier)).start();
// }
}
static class readNum implements Runnable{
private int id;
private CyclicBarrier cyc;
public readNum(int id, CyclicBarrier cyc){
this.id = id;
this.cyc = cyc;
}
@Override
public void run() {
synchronized (this){
System.out.println("id:"+id);
try {
cyc.await();
System.out.println("线程组任务" + id + "结束,其他任务继续");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}