死锁

转载地址:http://blog.csdn.net/qq_24653023/article/details/51764451


死锁的情况 

千万不要在使用中造成这种情况



[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package day13;  
  2.   
  3. class Test implements Runnable  
  4. {  
  5.     private boolean flag;  
  6.     Test(boolean flag)  
  7.     {  
  8.         this.flag = flag;  
  9.     }  
  10.   
  11.     public void run()  
  12.     {  
  13.           
  14.         if(flag)  
  15.         {  
  16.             while(true)  
  17.                 synchronized(MyLock.locka)  
  18.                 {  
  19.                     System.out.println(Thread.currentThread().getName()+"..if   locka....");  
  20.                     synchronized(MyLock.lockb)              {  
  21.                           
  22.                         System.out.println(Thread.currentThread().getName()+"..if   lockb....");  
  23.                     }  
  24.                 }  
  25.         }  
  26.         else  
  27.         {  
  28.             while(true)           
  29.                 synchronized(MyLock.lockb)  
  30.                 {  
  31.                     System.out.println(Thread.currentThread().getName()+"..else  lockb....");  
  32.                     synchronized(MyLock.locka)  
  33.                     {  
  34.                         System.out.println(Thread.currentThread().getName()+"..else   locka....");  
  35.                     }  
  36.                 }  
  37.         }  
  38.   
  39.     }  
  40.   
  41. }  
  42. class MyLock  
  43. {  
  44.     public static final Object locka = new Object();  
  45.     public static final Object lockb = new Object();  
  46. }  
  47. public class DeadLockTest   
  48. {  
  49.     public static void main(String[] args)   
  50.     {  
  51.         Test a = new Test(true);  
  52.         Test b = new Test(false);  
  53.   
  54.         Thread t1 = new Thread(a);  
  55.         Thread t2 = new Thread(b);  
  56.         t1.start();  
  57.         t2.start();  
  58.     }  
  59. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值