-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCyclicBarrierTest4.java
More file actions
43 lines (37 loc) · 1.22 KB
/
Copy pathCyclicBarrierTest4.java
File metadata and controls
43 lines (37 loc) · 1.22 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
package four;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author yangxp <i>练习CyclicBarrier(int parties, Runnable barrierAction)的用法</i>
*/
public class CyclicBarrierTest4 {
private static final Logger LOGGER = LoggerFactory.getLogger("Test4");
public static void main(String[] args) throws InterruptedException {
// 构造器:设置屏障放开前做的事情
CyclicBarrier barrier3 = new CyclicBarrier(2, () -> {
LOGGER.info("屏障放开,[屏障线程]先运行!");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
LOGGER.info("[屏障线程]的事情做完了!");
});
Thread.sleep(1000);
for (int i = 0; i < 2; i++) {
new Thread(() -> {
LOGGER.info(Thread.currentThread().getName() + " 等待屏障放开");
try {
barrier3.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
LOGGER.info(Thread.currentThread().getName() + "开始干活...干活结束");
}).start();
}
}
}