-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounter0.java
More file actions
63 lines (53 loc) · 1.4 KB
/
Counter0.java
File metadata and controls
63 lines (53 loc) · 1.4 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
61
62
63
package multithreading.Synchronization;
/*
For each thread
1. Retrieve the current value of c.
2. Increment the retrieved value by 1.
3. Store the incremented value back in c.
Global view
1. Thread A: Retrieve c.
2. Thread B: Retrieve c.
3. Thread A: Increment retrieved value; result is 1.
4. Thread B: Decrement retrieved value; result is -1.
5. Thread A: Store result in c; c is now 1.
6. Thread B: Store result in c; c is now -1.
*/
public class Counter0 {
// atomic variable
// private AtomicInteger c = 0;
private int c = 0;
public static void main(String[] args) throws InterruptedException {
Counter0 counter = new Counter0();
for (int i = 0; i < 1000; i++) {
Thread t1 = new Thread(() -> counter.increment());
Thread t2 = new Thread(() -> counter.decrement());
t1.start();
t2.start();
t1.join();
t2.join();
// get the value after t1, t2 finished
System.out.println(counter.value());
}
}
// public void increment() {
// c++;
// }
//
// public void decrement() {
// c--;
// }
//
// public int value() {
// return c;
// }
// synchronized methods.
public synchronized void increment() {
c++;
}
public synchronized void decrement() {
c--;
}
public synchronized int value() {
return c;
}
}