/
Reveal.java
63 lines (58 loc) · 1.86 KB
/
Reveal.java
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
61
62
63
package concurrent.reorder;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @Author: kkyeer
* @Description: 展示指令重排序导致的结果:可能输出根据源代码无法推导的结果
* @Date:Created in 17:16 2019/7/7
* @Modified By:
*/
class Reveal extends Thread {
private volatile int a = 0, b = 0;
private int x = 0, y = 0;
private ExecutorService executorService;
/**
* Allocates a new {@code Thread} object. This constructor has the same
* effect as {@linkplain #Thread(ThreadGroup, Runnable, String) Thread}
* {@code (null, null, gname)}, where {@code gname} is a newly generated
* name. Automatically generated names are of the form
* {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
*/
public Reveal(ExecutorService executorService) {
this.executorService = executorService;
}
@Override
public void run() {
Thread thread1 = new Thread(
() -> {
a = 1;
y = b;
}
);
Thread thread2 = new Thread(
() -> {
b = 1;
x = a;
}
);
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
// 这里的分析详见README
if (x == 0 && y == 0) {
System.err.println("Wrong,x = 0 and y = 0");
executorService.shutdown();
}
}
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
while (true) {
executorService.submit(new Reveal(executorService));
}
}
}