package com.cky.bean; /**
* Created by edison on 2017/12/3.
*/
public class MyObject {
private String usrName = "1";
private String pwd ="11";
public void setValue(String u, String p) {
this.usrName = u;
if (Thread.currentThread().getName().equals("a")) {
System.out.println("停止a线程");
Thread.currentThread().suspend();
}
this.pwd =p;
} public void printNameAndPwd() {
System.out.println(usrName +":"+pwd);
}
}
package com.cky.test; import com.cky.bean.MyObject; /**
* Created by edison on 2017/12/3.
*/
public class Run2 {
public static void main(String[] args) {
try {
final MyObject myObject = new MyObject();
Thread th1=new Thread(){
@Override
public void run() {
super.run();
myObject.setValue("a", "aa");
}
};
th1.setName("a");
th1.start();
Thread.sleep(500);
Thread th2=new Thread(){
@Override
public void run() {
super.run();
myObject.printNameAndPwd();
}
};
th2.start();
} catch (InterruptedException e) {
e.printStackTrace();
} }
}
C:\itsoft\jdk\bin\java -Didea.launcher.port=7538 "-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.Run2
停止a线程
a:11
结果不同步,程序中使用suspend()得注意。