forked from solvery/lang-features
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvolatile_2.java
More file actions
47 lines (40 loc) · 818 Bytes
/
Copy pathvolatile_2.java
File metadata and controls
47 lines (40 loc) · 818 Bytes
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
public class volatile_2 {
//jprivate volatile int i = 0;
//jprivate volatile int j = 0;
private int i = 0;
private int j = 0;
public long exceptionCount = 0;
public void f1() {
for(int k = 0; k < Integer.MAX_VALUE; k++) {
i = k;
j = i;
}
}
public void f2() {
while (true) {
if(j > i) {
exceptionCount++;
}
}
}
public static void main(String[] args) {
final volatile_2 volatileTest = new volatile_2();
new Thread(new Runnable() {
@Override
public void run() {
volatileTest.f1();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
volatileTest.f2();
}
}).start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
System.out.println("exceptionCount:" + volatileTest.exceptionCount);
}
}