-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestSync.java
More file actions
33 lines (29 loc) · 922 Bytes
/
TestSync.java
File metadata and controls
33 lines (29 loc) · 922 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
/** The "Lost Update" problem
* a classic concurrency problem.
* Actually, IT DOESN'T WORK as expected!
*/
public class TestSync implements Runnable {
private int balance;
// each thread runs 50 times,
// increasing the balance on
// each iteration
public void run() {
for (int i = 0; i < 50; i++) {
increment();
}
}
// try to comment/uncomment 'synchronized'
// and compare the results
public synchronized void increment() {
int i = balance;
// Here is the crucial part! We increment the balance by
// adding 1 to whatever the value of balance was AT THE
// TIME WE READ IT (rather than adding 1 to whatever
// the CURRENT value is)
// (this operation is deliberately made non-atomic
// (although it still not be atomic even in case of:
// ...increment() { balance++; } ))
balance = i + 1;
System.out.println("balance is " + balance + ". " + Thread.currentThread().getName());
}
}