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

一个线程进入一个对象的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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值