-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadDemo4.java
More file actions
66 lines (54 loc) · 1.52 KB
/
Copy pathThreadDemo4.java
File metadata and controls
66 lines (54 loc) · 1.52 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
64
65
66
package secondPart;
/**
* @author yangxp
* @date 2017年8月3日 下午5:24:19
* <p>
* 脏读 <i>dirtyRead</i> 注意取值的时候也可能出现意外:在读取实例变量的时候,此值已经别其他线程更改过了
* @See ThreadDemo3
*/
public class ThreadDemo4 {
public static void main(String[] args) {
PublicVar publicVar = new PublicVar();
ThreadDemo4Test threadDemo4Test = new ThreadDemo4Test(publicVar);
threadDemo4Test.start();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
publicVar.getValue();
}
}
class PublicVar {
public String username = "A";
public String password = "AA";
public synchronized void setValue(String username, String password) {
try {
this.username = username;
Thread.sleep(5000);
this.username = password;
System.out.println(Thread.currentThread().getName() + " username = " + username + " password " + password);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 该方法不是同步的
*/
public void getValue() {
System.out.println(Thread.currentThread().getName() + " username = " + username + " password " + password);
}
}
class ThreadDemo4Test extends Thread {
private PublicVar publicVar;
public ThreadDemo4Test(PublicVar publicVar) {
super();
this.publicVar = publicVar;
}
@Override
public void run() {
super.run();
publicVar.setValue("B", "BB");
}
}