这两个方法使用不当,容易造成公共的同步对象的独占,使得其他线程无法访问公共的同步对象
测试
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:itsoftjdkinjava -Didea.launcher.port=7535 "-Didea.launcher.bin.path=C:itsoftideaIntelliJ IDEA 2016.3.3in" -Dfile.encoding=UTF-8 -classpath "C:itsoftjdkjrelibcharsets.jar;C:itsoftjdkjrelibdeploy.jar;C:itsoftjdkjrelibextaccess-bridge-32.jar;C:itsoftjdkjrelibextcldrdata.jar;C:itsoftjdkjrelibextdnsns.jar;C:itsoftjdkjrelibextjaccess.jar;C:itsoftjdkjrelibextjfxrt.jar;C:itsoftjdkjrelibextlocaledata.jar;C:itsoftjdkjrelibext
ashorn.jar;C:itsoftjdkjrelibextsunec.jar;C:itsoftjdkjrelibextsunjce_provider.jar;C:itsoftjdkjrelibextsunmscapi.jar;C:itsoftjdkjrelibextsunpkcs11.jar;C:itsoftjdkjrelibextzipfs.jar;C:itsoftjdkjrelibjavaws.jar;C:itsoftjdkjrelibjce.jar;C:itsoftjdkjrelibjfr.jar;C:itsoftjdkjrelibjfxswt.jar;C:itsoftjdkjrelibjsse.jar;C:itsoftjdkjrelibmanagement-agent.jar;C:itsoftjdkjrelibplugin.jar;C:itsoftjdkjrelib
esources.jar;C:itsoftjdkjrelib
t.jar;C:多线程核心技术第一章outproduction第一章;C:itsoftideaIntelliJ IDEA 2016.3.3libidea_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方法内部时,同步锁没有被释放