当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法

来源:互联网 发布:淘宝店铺被永久查封 编辑:程序博客网 时间:2024/06/05 00:38

一个线程进入一个对象的synchronized()方法后,其他线程是否可以进入此对象的其他方法取决于方法本身,如果该方法是非synchronized()方法,那么是可以访问的,如果是synchronized()方法,那么不能访问。示例如下:

[java] view plain copy
  1. package synchLockTest;  
  2.   
  3. class Test{  
  4.     public synchronized void synchronizedMethod(){  
  5.         System.out.println("begin calling sychronizedMethod...");  
  6.         try{  
  7.             Thread.sleep(10000);  
  8.         }catch(InterruptedException e){  
  9.             e.printStackTrace();  
  10.         }  
  11.         System.out.println("finish calling  sychronizedMethod...");  
  12.     }  
  13.       
  14.   
  15.     public void generalMethod(){  
  16.         System.out.println("call generalMethod...");  
  17.     }  
  18. }  
  19. public class MultiThread {  
  20.         static final Test t=new Test();  
  21.     public static void main(String[] args) {  
  22.             Thread t1=new Thread(){  
  23.                 public void run(){  
  24.                     t.synchronizedMethod();  
  25.                 }  
  26.             };  
  27.               
  28.             Thread t2=new Thread(){  
  29.                 public void run(){  
  30.                     t.generalMethod();  
  31.                 }  
  32.             };  
  33.   
  34.             t1.start();  
  35.             t2.start();  
  36.   
  37.     }  
  38.   
  39. }  



输出:
begin calling sychronizedMethod...
call generalMethod...
finish calling  sychronizedMethod...

从上例可以看出,线程t1在调用synchronized()方法的过程中,线程t2仍然可以访问同一个对象的非sychronized()方法。

若将代码稍稍做改动:
在方法generalMethod()前加一个synchronized的修饰符。因为线程t1调用synchronized void synchronizedMethod()的时候(即线程t1获得了对象锁),t2是无法调用该方法的。修改后的代码如下:
[java] view plain copy
  1. package synchLockTest;  
  2.   
  3. class Test{  
  4.     public synchronized void synchronizedMethod(){  
  5.         System.out.println("begin calling sychronizedMethod...");  
  6.         try{  
  7.             Thread.sleep(10000);  
  8.         }catch(InterruptedException e){  
  9.             e.printStackTrace();  
  10.         }  
  11.         System.out.println("finish calling  sychronizedMethod...");  
  12.     }  
  13.       
  14.   
  15.     public synchronized void generalMethod(){  
  16.         System.out.println("call generalMethod...");  
  17.     }  
  18. }  
  19. public class MultiThread {  
  20.         static final Test t=new Test();  
  21.     public static void main(String[] args) {  
  22.             Thread t1=new Thread(){  
  23.                 public void run(){  
  24.                     t.synchronizedMethod();  
  25.                 }  
  26.             };  
  27.               
  28.             Thread t2=new Thread(){  
  29.                 public void run(){  
  30.                     t.generalMethod();  
  31.                 }  
  32.             };  
  33.   
  34.             t1.start();  
  35.             t2.start();  
  36.   
  37.     }  
  38.   
  39. }  
结果如下:
begin calling sychronizedMethod...
finish calling  sychronizedMethod...
call generalMethod...

如果其他方法是静态的方法,它用的同步锁是当前类的字节码,与非静态的方法不能同步,因此,静态方法可以被调用,实例如下:
[java] view plain copy
  1. package synchLockTest;  
  2.   
  3. class Test{  
  4.     public synchronized void synchronizedMethod(){  
  5.         System.out.println("begin calling sychronizedMethod...");  
  6.         try{  
  7.             Thread.sleep(10000);  
  8.         }catch(InterruptedException e){  
  9.             e.printStackTrace();  
  10.         }  
  11.         System.out.println("finish calling  sychronizedMethod...");  
  12.     }  
  13.       
  14.   
  15.     public synchronized static void generalMethod(){  
  16.         System.out.println("call generalMethod...");  
  17.     }  
  18. }  
  19. public class MultiThread {  
  20.         static final Test t=new Test();  
  21.     public static void main(String[] args) {  
  22.             Thread t1=new Thread(){  
  23.                 public void run(){  
  24.                     t.synchronizedMethod();  
  25.                 }  
  26.             };  
  27.               
  28.             Thread t2=new Thread(){  
  29.                 public void run(){  
  30.                     t.generalMethod();  
  31.                 }  
  32.             };  
  33.   
  34.             t1.start();  
  35.             t2.start();  
  36.   
  37.     }  
  38.   
  39. }  

结果如下:
begin calling sychronizedMethod...
call generalMethod...
finish calling  sychronizedMethod...
0 0