若多个线程共同访问1个对象中的实例变量,那么有可能出现非线程安全的问题。
测试如下
1 package com.cky.bean; 2 3 /** 4 * Created by chenkaiyang on 2017/12/4. 5 */ 6 public class SelfPrivateNum { 7 int num =0; 8 public void addI(String usrName) { 9 try { 10 if (usrName.equals("a")) { 11 num =100; 12 System.out.println("a over"); 13 Thread.sleep(2000); 14 } else { 15 num= 200; 16 System.out.println("b over"); 17 18 } 19 System.out.println(usrName + " num="+ num); 20 } catch (InterruptedException e) { 21 e.printStackTrace(); 22 } 23 } 24 }
package com.cky.thread; import com.cky.bean.SelfPrivateNum; /** * Created by chenkaiyang on 2017/12/4. */ public class ThreadA extends Thread{ private SelfPrivateNum sn; public ThreadA (SelfPrivateNum sn) { this.sn = sn; } @Override public void run() { super.run(); sn.addI("a"); } }
1 package com.cky.thread; 2 3 import com.cky.bean.SelfPrivateNum; 4 5 /** 6 * Created by chenkaiyang on 2017/12/4. 7 */ 8 public class ThreadB extends Thread{ 9 private SelfPrivateNum sn; 10 public ThreadB (SelfPrivateNum sn) { 11 this.sn = sn; 12 } 13 @Override 14 public void run() { 15 super.run(); 16 sn.addI("b"); 17 } 18 }
package com.cky.test; import com.cky.bean.SelfPrivateNum; import com.cky.thread.ThreadA; import com.cky.thread.ThreadB; /** * Created by chenkaiyang on 2017/12/2. */ public class Test { public static void main(String[] args){ SelfPrivateNum sn = new SelfPrivateNum(); ThreadA threadA = new ThreadA(sn); ThreadB threadB = new ThreadB(sn); threadA.start(); threadB.start(); } }
D:itjdk1.8injava -Didea.launcher.port=7533 "-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 a over b over b num=200 a num=200 Process finished with exit code 0
结果分析:出现了非线程安全的问题,那如何解决呢?
使用synchronized关键字
1 package com.cky.bean; 2 3 /** 4 * Created by chenkaiyang on 2017/12/4. 5 */ 6 public class SelfPrivateNum { 7 int num =0; 8 synchronized public void addI(String usrName) { 9 try { 10 if (usrName.equals("a")) { 11 num =100; 12 System.out.println("a over"); 13 Thread.sleep(2000); 14 } else { 15 num= 200; 16 System.out.println("b over"); 17 18 } 19 System.out.println(usrName + " num="+ num); 20 } catch (InterruptedException e) { 21 e.printStackTrace(); 22 } 23 } 24 }
D:itjdk1.8injava -Didea.launcher.port=7534 "-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 a over a num=100 b over b num=200 Process finished with exit code 0
结果分析:使用synchronized关键字,当a拿到锁之后,只有当前执行完毕释放锁的时候,b才能拿到锁,解决了线程安全。而且代码的执行是同步的,因为只有1个对象。