1. 程式人生 > 其它 >面試-計算機網路

面試-計算機網路

死鎖

  • 多個執行緒各自佔有一些共享資源,並且互相等待其他執行緒佔有的資源才能執行,而導致兩個或者多個執行緒都在等待對方釋放資源,都停止執行的情形。某一個同步塊同時擁有 ”兩個以上物件的鎖“ 時,就可能會發生 “死鎖” 的問題。

package com.thread.lock;


//死鎖:多個執行緒互相抱著對方需要的資源,然後形成僵持
public class DeadLock {

   public static void main(String[] args) {
        Makeup girl1 = new Makeup(0,"小紅");
        Makeup girl2 = new Makeup(1,"小婷");

        girl1.start();
        girl2.start();
  }

}

//口紅
class Lipstick{

}

//鏡子
class Mirror{

}

class Makeup extends Thread{

   //資源各只有一份,用static來保證只有一份
   static Lipstick lipstick = new Lipstick();
   static Mirror mirror = new Mirror();

   int choice;
   String girlName;

   Makeup(int choice,String girlName){
       this.choice = choice;
       this.girlName = girlName;
  }


   @Override
   public void run() {

       try {
           makeup();
      } catch (InterruptedException e) {
           e.printStackTrace();
      }

  }

   //化妝
   private void makeup() throws InterruptedException {
       if(choice==0){
           synchronized (lipstick){//獲得口紅的鎖
               System.out.println(this.girlName+"獲得口紅");
               Thread.sleep(1000);

               synchronized (mirror){//一秒鐘後想獲得鏡子的鎖
                   System.out.println(this.girlName+"獲得鏡子");
              }
          }
      }
       else{
           synchronized (mirror){//獲得鏡子的鎖
               System.out.println(this.girlName+"獲得鏡子");
               Thread.sleep(2000);

               synchronized (lipstick){//一秒鐘後想獲得口紅的鎖
                   System.out.println(this.girlName+"獲得口紅");
              }
          }
      }
       

  }

}

小紅獲得口紅 小婷獲得鏡子 (死鎖)

 

package com.thread.lock;


//死鎖:多個執行緒互相抱著對方需要的資源,然後形成僵持
public class DeadLock {

   public static void main(String[] args) {
        Makeup girl1 = new Makeup(0,"小紅");
        Makeup girl2 = new Makeup(1,"小婷");

        girl1.start();
        girl2.start();
  }

}

//口紅
class Lipstick{

}

//鏡子
class Mirror{

}

class Makeup extends Thread{

   //資源各只有一份,用static來保證只有一份
   static Lipstick lipstick = new Lipstick();
   static Mirror mirror = new Mirror();

   int choice;
   String girlName;

   Makeup(int choice,String girlName){
       this.choice = choice;
       this.girlName = girlName;
  }


   @Override
   public void run() {

       try {
           makeup();
      } catch (InterruptedException e) {
           e.printStackTrace();
      }

  }

   //化妝
   private void makeup() throws InterruptedException {
       if(choice==0){
           synchronized (lipstick){//獲得口紅的鎖
               System.out.println(this.girlName+"獲得口紅");
               Thread.sleep(1000);
          }
           synchronized (mirror){//一秒鐘後想獲得鏡子的鎖
               System.out.println(this.girlName+"獲得鏡子");
          }
      }
       else{
           synchronized (mirror){//獲得鏡子的鎖
               System.out.println(this.girlName+"獲得鏡子");
               Thread.sleep(2000);
          }
           synchronized (lipstick){//2秒鐘後想獲得口紅的鎖
               System.out.println(this.girlName+"獲得口紅");
          }

      }


  }

}

小紅獲得口紅 小婷獲得鏡子 小婷獲得口紅 小紅獲得鏡子 (死鎖解決)

 

 

產生死鎖的四個必要條件:

  1. 互斥條件:一個資源每次只能被一個程序使用。

  2. 請求與保持條件: 一個程序因請求資源而阻塞時,對已獲得的資源保持不放。

  3. 不剝奪條件:程序已獲得的資源,在末使用完之前,不能強行剝奪。

  4. 迴圈等待條件:若干程序之間形成一種頭尾相接的迴圈等待資源關係。

上面列出了死鎖的四個必要條件,我們只要想辦法破其中的任意一個或多個條件就可以避免死鎖發生。