成功的路上总是离不开贵人的帮助,名师的指点和小人的刺激。

莫怕,过了桥,就翻篇了

1.8.3suspend与resume方法的缺点--不同步

 1 package com.cky.bean;
 2 
 3 /**
 4  * Created by edison on 2017/12/3.
 5  */
 6 public class MyObject {
 7     private String usrName = "1";
 8     private String pwd ="11";
 9     public void setValue(String u, String p) {
10         this.usrName = u;
11         if (Thread.currentThread().getName().equals("a")) {
12             System.out.println("停止a线程");
13             Thread.currentThread().suspend();
14         }
15         this.pwd =p;
16     }
17 
18     public void printNameAndPwd() {
19         System.out.println(usrName +":"+pwd);
20     }
21 }
 1 package com.cky.test;
 2 
 3 import com.cky.bean.MyObject;
 4 
 5 /**
 6  * Created by edison on 2017/12/3.
 7  */
 8 public class Run2 {
 9     public static void main(String[] args) {
10         try {
11             final MyObject myObject = new MyObject();
12             Thread th1=new Thread(){
13                 @Override
14                 public void run() {
15                     super.run();
16                     myObject.setValue("a", "aa");
17                 }
18             };
19             th1.setName("a");
20             th1.start();
21             Thread.sleep(500);
22             Thread th2=new Thread(){
23                 @Override
24                 public void run() {
25                     super.run();
26                     myObject.printNameAndPwd();
27                 }
28             };
29             th2.start();
30         } catch (InterruptedException e) {
31             e.printStackTrace();
32         }
33 
34 
35     }
36 }
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()得注意。

 

posted on 2017-12-03 17:48  痞子陈2016  阅读(190)  评论(0编辑  收藏  举报

导航