-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCyclicBarrierTest2.java
More file actions
60 lines (50 loc) · 1.61 KB
/
Copy pathCyclicBarrierTest2.java
File metadata and controls
60 lines (50 loc) · 1.61 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
49
50
51
52
53
54
55
56
57
58
59
60
package four;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author yangxp
*<p> reset 的使用 等待过程中reset 会报错 BrokenBarrierException
*/
public class CyclicBarrierTest2 {
private static final Logger LOGGER = LoggerFactory.getLogger("Test2");
public static void main(String[] args) throws InterruptedException {
CyclicBarrier barrier2 = new CyclicBarrier(2);
LOGGER.info("如果是一个初始的CyclicBarrier,则reset()之后,什么也不会发生");
barrier2.reset();
System.out.println();
Thread.sleep(1000);
ExecutorService ex2 = Executors.newCachedThreadPool();
for (int i = 0; i < 2; i++) {
ex2.submit(() -> {
try {
barrier2.await();
LOGGER.info("222屏障已经打开.");
} catch (InterruptedException e) {
LOGGER.info("222被中断");
} catch (BrokenBarrierException e) {
LOGGER.info("222被重置");
}
});
}
barrier2.reset();
Thread.sleep(1000);
System.out.println();
ex2.submit(() -> {
try {
barrier2.await();
LOGGER.info("333屏障已经打开.");
} catch (InterruptedException e) {
LOGGER.info("333被中断");
} catch (BrokenBarrierException e) {
LOGGER.info("在等待过程中,执行reset()方法,等待的线程抛出BrokenBarrierException异常,并不再等待");
}
});
Thread.sleep(1000);
barrier2.reset();
ex2.shutdown();
}
}