-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAtomicityTest.java
More file actions
45 lines (34 loc) · 888 Bytes
/
AtomicityTest.java
File metadata and controls
45 lines (34 loc) · 888 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
package concurrency;
import java.util.concurrent.*;
import java.util.concurrent.locks.*;
/**
* RUN:
* javac concurrency/AtomicityTest.java && java concurrency.AtomicityTest
*
* OUTPUT:
*
*/
public class AtomicityTest implements Runnable {
private int i = 0;
public int getValue() { return i; }
private synchronized void evenIncrement() {
i++; ++i;
}
public void run() {
while (true) {
evenIncrement();
}
}
public static void main(String[] args) {
ExecutorService exec = Executors.newCachedThreadPool();
AtomicityTest at = new AtomicityTest();
exec.execute(at);
while (true) {
int val = at.getValue();
if (val % 2 != 0) {
System.out.println(val);
System.exit(0);
}
}
}
}