虽然在赋值的时候进行了同步,但是在取值的时候会出问题,这就叫做脏读。
发生脏读说明在读取实例变量的值的时候,此值已经被其他线程改过了。
测试如下
package com.cky.bean;
/**
* Created by chenkaiyang on 2017/12/5.
*/
public class MyObject {
public String usrName= "A";
public String pwd = "AA";
synchronized public void setValue(String usrName, String pwd) {
try {
this.usrName = usrName;
Thread.sleep(5000);
this.pwd = pwd;
System.out.println("setValue method name=" + Thread.currentThread().getName() +" usrName="+ usrName+ " pwd="+ pwd);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void getValue() {
System.out.println("getValue method name=" + Thread.currentThread().getName() +" usrName="+ usrName+ " pwd="+ pwd);
}
}
1 package com.cky.thread; 2 3 import com.cky.bean.MyObject; 4 5 /** 6 * Created by chenkaiyang on 2017/12/5. 7 */ 8 public class ThreadA extends Thread{ 9 private MyObject mo; 10 public ThreadA(MyObject mo) { 11 super(); 12 this.mo = mo; 13 } 14 15 @Override 16 public void run() { 17 super.run(); 18 mo.setValue("B", "BB"); 19 } 20 }
package com.cky.test; import com.cky.bean.MyObject; import com.cky.thread.ThreadA; /** * Created by chenkaiyang on 2017/12/2. */ public class Test { public static void main(String[] args){ try { MyObject myObject = new MyObject(); ThreadA threadA = new ThreadA(myObject); threadA.start(); Thread.sleep(2000);//打印结果受到这个睡眠时间大小的影响 myObject.getValue(); } catch (InterruptedException e) { e.printStackTrace(); } } }
D:itjdk1.8injava -Didea.launcher.port=7532 "-Didea.launcher.bin.path=D:itideaIntelliJ IDEA 2016.3.3in" -Dfile.encoding=UTF-8 -classpath "D:itjdk1.8jrelibcharsets.jar;D:itjdk1.8jrelibdeploy.jar;D:itjdk1.8jrelibextaccess-bridge-64.jar;D:itjdk1.8jrelibextcldrdata.jar;D:itjdk1.8jrelibextdnsns.jar;D:itjdk1.8jrelibextjaccess.jar;D:itjdk1.8jrelibextjfxrt.jar;D:itjdk1.8jrelibextlocaledata.jar;D:itjdk1.8jrelibext ashorn.jar;D:itjdk1.8jrelibextsunec.jar;D:itjdk1.8jrelibextsunjce_provider.jar;D:itjdk1.8jrelibextsunmscapi.jar;D:itjdk1.8jrelibextsunpkcs11.jar;D:itjdk1.8jrelibextzipfs.jar;D:itjdk1.8jrelibjavaws.jar;D:itjdk1.8jrelibjce.jar;D:itjdk1.8jrelibjfr.jar;D:itjdk1.8jrelibjfxswt.jar;D:itjdk1.8jrelibjsse.jar;D:itjdk1.8jrelibmanagement-agent.jar;D:itjdk1.8jrelibplugin.jar;D:itjdk1.8jrelib esources.jar;D:itjdk1.8jrelib t.jar;F:springboot hreaddemooutproduction hreaddemo;D:itideaIntelliJ IDEA 2016.3.3libidea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test getValue method name=main usrName=B pwd=AA setValue method name=Thread-0 usrName=B pwd=BB Process finished with exit code 0
出现结果分析:
出现脏读是因为getValue方法不是同步的,所以可以在任意时候进行调用。
解决方案:加上synchronized
synchronized public void getValue() { System.out.println("getValue method name=" + Thread.currentThread().getName() +" usrName="+ usrName+ " pwd="+ pwd); }
D:itjdk1.8injava -Didea.launcher.port=7535 "-Didea.launcher.bin.path=D:itideaIntelliJ IDEA 2016.3.3in" -Dfile.encoding=UTF-8 -classpath "D:itjdk1.8jrelibcharsets.jar;D:itjdk1.8jrelibdeploy.jar;D:itjdk1.8jrelibextaccess-bridge-64.jar;D:itjdk1.8jrelibextcldrdata.jar;D:itjdk1.8jrelibextdnsns.jar;D:itjdk1.8jrelibextjaccess.jar;D:itjdk1.8jrelibextjfxrt.jar;D:itjdk1.8jrelibextlocaledata.jar;D:itjdk1.8jrelibext ashorn.jar;D:itjdk1.8jrelibextsunec.jar;D:itjdk1.8jrelibextsunjce_provider.jar;D:itjdk1.8jrelibextsunmscapi.jar;D:itjdk1.8jrelibextsunpkcs11.jar;D:itjdk1.8jrelibextzipfs.jar;D:itjdk1.8jrelibjavaws.jar;D:itjdk1.8jrelibjce.jar;D:itjdk1.8jrelibjfr.jar;D:itjdk1.8jrelibjfxswt.jar;D:itjdk1.8jrelibjsse.jar;D:itjdk1.8jrelibmanagement-agent.jar;D:itjdk1.8jrelibplugin.jar;D:itjdk1.8jrelib esources.jar;D:itjdk1.8jrelib t.jar;F:springboot hreaddemooutproduction hreaddemo;D:itideaIntelliJ IDEA 2016.3.3libidea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test setValue method name=Thread-0 usrName=B pwd=BB getValue method name=main usrName=B pwd=BB