forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccount.java
More file actions
55 lines (43 loc) · 1.28 KB
/
Account.java
File metadata and controls
55 lines (43 loc) · 1.28 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
package com.basic;
import java.util.concurrent.TimeUnit;
/**
* @program JavaBooks
* @description: 对业务写方法加锁,对业务读方法不加锁,容易产生脏读问题
* @author: mf
* @create: 2019/12/26 21:41
*/
public class Account {
String name;
double balance;
public synchronized void set(String name, double balance) {
this.name = name;
/**
* 故意设置2秒,让name和balance之间,受到其他事务的影响,产生脏读
*/
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.balance = balance;
}
public /*synchronized*/ double getBalance(String name) {
return this.balance;
}
public static void main(String[] args) {
Account a = new Account();
new Thread(() -> a.set("zhangsan", 100.0)).start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(a.getBalance("zhangsan"));
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(a.getBalance("shangsan"));
}
}