标签:结果 set source ica 建议 encoding pwd product end
使用stop释放锁将会导致数据出现不一致的结果。
测试如下
1 package com.cky.bean; 2 3 /** 4 * Created by edison on 2017/12/3. 5 */ 6 public class SynchroizeObject { 7 private String usrname="a"; 8 private String pwd ="aa"; 9 10 public String getUsrname() { 11 return usrname; 12 } 13 14 public void setUsrname(String usrname) { 15 this.usrname = usrname; 16 } 17 18 public String getPwd() { 19 return pwd; 20 } 21 22 public void setPwd(String pwd) { 23 this.pwd = pwd; 24 } 25 26 synchronized public void printString(String usrname, String pwd) { 27 try { 28 this.usrname = usrname; 29 Thread.sleep(100000); 30 this.pwd = pwd; 31 } catch (InterruptedException e) { 32 e.printStackTrace(); 33 } 34 } 35 }
1 package com.cky.thread; 2 3 import com.cky.bean.SynchroizeObject; 4 5 /** 6 * Created by edison on 2017/12/3. 7 */ 8 public class MyThread extends Thread{ 9 private SynchroizeObject so; 10 public MyThread (SynchroizeObject so) { 11 super(); 12 this.so = so; 13 } 14 @Override 15 public void run() { 16 so.printString("b","bb"); 17 } 18 }
package com.cky.test; import com.cky.bean.SynchroizeObject; import com.cky.thread.MyThread; /** * Created by edison on 2017/12/3. */ public class Test { public static void main(String[] args) { try { SynchroizeObject so = new SynchroizeObject(); MyThread myThread = new MyThread(so); myThread.start(); Thread.sleep(2000); myThread.stop(); System.out.println(so.getUsrname()+ "..."+ so.getPwd()); } catch (InterruptedException e) { e.printStackTrace(); } } }
C:\itsoft\jdk\bin\java -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:\itsoft\idea\IntelliJ IDEA 2016.3.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\itsoft\jdk\jre\lib\charsets.jar;C:\itsoft\jdk\jre\lib\deploy.jar;C:\itsoft\jdk\jre\lib\ext\access-bridge-32.jar;C:\itsoft\jdk\jre\lib\ext\cldrdata.jar;C:\itsoft\jdk\jre\lib\ext\dnsns.jar;C:\itsoft\jdk\jre\lib\ext\jaccess.jar;C:\itsoft\jdk\jre\lib\ext\jfxrt.jar;C:\itsoft\jdk\jre\lib\ext\localedata.jar;C:\itsoft\jdk\jre\lib\ext\nashorn.jar;C:\itsoft\jdk\jre\lib\ext\sunec.jar;C:\itsoft\jdk\jre\lib\ext\sunjce_provider.jar;C:\itsoft\jdk\jre\lib\ext\sunmscapi.jar;C:\itsoft\jdk\jre\lib\ext\sunpkcs11.jar;C:\itsoft\jdk\jre\lib\ext\zipfs.jar;C:\itsoft\jdk\jre\lib\javaws.jar;C:\itsoft\jdk\jre\lib\jce.jar;C:\itsoft\jdk\jre\lib\jfr.jar;C:\itsoft\jdk\jre\lib\jfxswt.jar;C:\itsoft\jdk\jre\lib\jsse.jar;C:\itsoft\jdk\jre\lib\management-agent.jar;C:\itsoft\jdk\jre\lib\plugin.jar;C:\itsoft\jdk\jre\lib\resources.jar;C:\itsoft\jdk\jre\lib\rt.jar;C:\多线程核心技术\第一章\out\production\第一章;C:\itsoft\idea\IntelliJ IDEA 2016.3.3\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test
b...aa
Process finished with exit code 0
综上所述,表明stop方法会导致数据不同步,不建议使用
标签:结果 set source ica 建议 encoding pwd product end
原文地址:http://www.cnblogs.com/edison20161121/p/7954764.html