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

莫怕,过了桥,就翻篇了

1.8.2suspend与resume方法的缺点-独占

这两个方法使用不当,容易造成公共的同步对象的独占,使得其他线程无法访问公共的同步对象

测试

 1 package com.cky.bean;
 2 
 3 /**
 4  * Created by edison on 2017/12/3.
 5  */
 6 public class SynchroObject{
 7 
 8     synchronized  public void printString() {
 9         System.out.println("begin");
10         if (Thread.currentThread().getName().equals("a")) {
11             System.out.println("a线程永远suspend");
12             Thread.currentThread().suspend();
13         }
14         System.out.println("end");
15     }
16 }
 1 package com.cky.test;
 2 
 3 import com.cky.bean.SynchroObject;
 4 
 5 /**
 6  * Created by edison on 2017/12/3.
 7  */
 8 public class Run {
 9     public static void main(String[] args) {
10         try {
11             final SynchroObject so = new SynchroObject();
12             Thread th1 = new Thread() {
13                 @Override
14                 public void run() {
15                     so.printString();
16                 }
17             };
18             th1.setName("a");
19             th1.start();
20             Thread.sleep(2000);
21             Thread th2 = new Thread() {
22                 @Override
23                 public void run() {
24                     System.out.println("thread2线程启动了,但是不能进入println方法,因为线程已经被a锁定且暂停");
25                     so.printString();
26                 }
27             };
28             th2.start();
29         } catch (InterruptedException e) {
30             e.printStackTrace();
31         }
32 
33     }
34 }
begin
a线程永远suspend
thread2线程启动了,但是不能进入println方法,因为线程已经被a锁定且暂停

下面是另一种独占锁

 

 1 package com.cky.thread;
 2 
 3 /**
 4  * Created by edison on 2017/12/3.
 5  */
 6 public class MyThread extends Thread{
 7     private long i=0;
 8 
 9     @Override
10     public void run() {
11         while(true) {
12             i++;
13         }
14     }
15 }
 1 package com.cky.test;
 2 
 3 import com.cky.thread.MyThread;
 4 
 5 /**
 6  * Created by edison on 2017/12/3.
 7  */
 8 public class Test2 {
 9     public static void main(String[] args) {
10         try {
11             MyThread th = new MyThread();
12             th.start();
13             Thread.sleep(1000);
14             th.suspend();
15             System.out.println("main end");
16         } catch (InterruptedException e) {
17             e.printStackTrace();
18         }
19     }
20 }
C:\itsoft\jdk\bin\java -Didea.launcher.port=7535 "-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.Test2
main end

Process finished with exit code 1

如果改成如下

 

 1 package com.cky.thread;
 2 
 3 /**
 4  * Created by edison on 2017/12/3.
 5  */
 6 public class MyThread2 extends  Thread{
 7     private long i=0;
 8 
 9     @Override
10     public void run() {
11         while(true) {
12             i++;
13             System.out.println("i="+i);
14         }
15     }
16 }
i=82093
i=82094
i=82095
i=82096
i=82097
i=82098
i=82099
i=82100
i=82101

控制台将不打印main end,

因为当程序运行到println方法内部时,同步锁没有被释放

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

导航