概念理解
对于对象的同步和异步的方法,我们在设计自己的程序的时候,一定要考虑问题的整体,不然就会出现数据不一致的错误,很经典的错误就是脏读(dirtyread)
案例
package cn.hfbin.sync004;
/**
* 业务整体需要使用完整的synchronized,保持业务的原子性。
* @author cn.hfbin
*
*/
public class DirtyRead {
private String username = "cn.hfbin";
private String password = "123";
public void setValue(String username, String password){
this.username = username;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.password = password;
System.out.println("setValue>>>>最终结果:username = " + username + " , password = " + password);
}
public void getValue(){
System.out.println("getValue>>>>方法得到:username = " + this.username + " , password = " + this.password);
}
public static void main(String[] args) throws Exception{
final DirtyRead fb = new DirtyRead();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
fb.setValue("hfbin", "456");
}
});
t1.start();
Thread.sleep(1000);
fb.getValue();
}
}
打印结果:
getValue>>>>方法得到:username = hfbin , password = 123
setValue>>>>最终结果:username = hfbin , password = 456
如果在 setValue()和getValue()方法同时加上synchronized修饰 打印结果如下:
getValue>>>>方法得到:username = hfbin , password = 456
setValue>>>>最终结果:username = hfbin , password = 456
注意:不能只在 setValue()和getValue()方法中任意一个加synchronized,否则还是会出现脏读现象
结论
在我们对一个对象的方法加锁的时候,需要考虑业务的整体性,即为setValue/getValue方法同时加锁synchronized同步关键字,保证业务(service)的原子性,不然会出现业务错误(也从侧面保证业务的一致性)。
源代码:https://github.com/hfbin/Thread_Socket/tree/master/Thread/sync004