死锁

    xiaoxiao2021-03-25  64

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

    死锁的情况 

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

    [cpp]  view plain  copy  print ? package day13;      class Test implements Runnable   {       private boolean flag;       Test(boolean flag)       {           this.flag = flag;       }          public void run()       {                      if(flag)           {               while(true)                   synchronized(MyLock.locka)                   {                       System.out.println(Thread.currentThread().getName()+"..if   locka....");                       synchronized(MyLock.lockb)              {                                                      System.out.println(Thread.currentThread().getName()+"..if   lockb....");                       }                   }           }           else           {               while(true)                            synchronized(MyLock.lockb)                   {                       System.out.println(Thread.currentThread().getName()+"..else  lockb....");                       synchronized(MyLock.locka)                       {                           System.out.println(Thread.currentThread().getName()+"..else   locka....");                       }                   }           }          }      }   class MyLock   {       public static final Object locka = new Object();       public static final Object lockb = new Object();   }   public class DeadLockTest    {       public static void main(String[] args)        {           Test a = new Test(true);           Test b = new Test(false);              Thread t1 = new Thread(a);           Thread t2 = new Thread(b);           t1.start();           t2.start();       }   }  
    转载请注明原文地址: https://ju.6miu.com/read-34861.html

    最新回复(0)