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

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

测试

 package com.cky.bean;

 /**
* Created by edison on 2017/12/3.
*/
public class SynchroObject{ synchronized public void printString() {
System.out.println("begin");
if (Thread.currentThread().getName().equals("a")) {
System.out.println("a线程永远suspend");
Thread.currentThread().suspend();
}
System.out.println("end");
}
}
 package com.cky.test;

 import com.cky.bean.SynchroObject;

 /**
* Created by edison on 2017/12/3.
*/
public class Run {
public static void main(String[] args) {
try {
final SynchroObject so = new SynchroObject();
Thread th1 = new Thread() {
@Override
public void run() {
so.printString();
}
};
th1.setName("a");
th1.start();
Thread.sleep(2000);
Thread th2 = new Thread() {
@Override
public void run() {
System.out.println("thread2线程启动了,但是不能进入println方法,因为线程已经被a锁定且暂停");
so.printString();
}
};
th2.start();
} catch (InterruptedException e) {
e.printStackTrace();
} }
}
begin
a线程永远suspend
thread2线程启动了,但是不能进入println方法,因为线程已经被a锁定且暂停

下面是另一种独占锁

 package com.cky.thread;

 /**
* Created by edison on 2017/12/3.
*/
public class MyThread extends Thread{
private long i=0; @Override
public void run() {
while(true) {
i++;
}
}
}
 package com.cky.test;

 import com.cky.thread.MyThread;

 /**
* Created by edison on 2017/12/3.
*/
public class Test2 {
public static void main(String[] args) {
try {
MyThread th = new MyThread();
th.start();
Thread.sleep(1000);
th.suspend();
System.out.println("main end");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
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

如果改成如下

 package com.cky.thread;

 /**
* Created by edison on 2017/12/3.
*/
public class MyThread2 extends Thread{
private long i=0; @Override
public void run() {
while(true) {
i++;
System.out.println("i="+i);
}
}
}
i=82093
i=82094
i=82095
i=82096
i=82097
i=82098
i=82099
i=82100
i=82101

控制台将不打印main end,

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

上一篇:再跟SQL谈一谈--基础篇


下一篇:nginx的配置说明