日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

線程死鎖的概念及其解決方法

作者:Qiddo 更新時間: 2023-12-12 編程語言

“死鎖”指的是:

多個線程各自占有一些共享資源,并且互相等待其他線程占有的資源才能進行,而導致兩個或者多個線程都在等待對方釋放資源,都停止執行的情形。

某一個同步塊需要同時擁有“兩個以上對象的鎖”時,就可能會發生“死鎖”的問題。

比如,“化妝線程”需要同時擁有“鏡子對象”、“口紅對象”才能運行同步塊。那么,實際運行時,“小丫的化妝線程”擁有了“鏡子對象”,“大丫的化妝線程”擁有了“口紅對象”,都在互相等待對方釋放資源,才能化妝。這樣,兩個線程就形成了互相等待,無法繼續運行的“死鎖狀態”。

?下面是案例的代碼演示

/**
 * 口紅類
 */
class Lipstick{

}


/**
 * 鏡子類
 */
class Mirror{

}


/**
 * 化妝線程類
 */
class Makeup extends Thread{
      private int flag; //flag=0:拿著口紅。flag!=0:拿著鏡子
      private String girlName;
      static Lipstick lipstick = new Lipstick();
      static Mirror mirror = new Mirror();


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

      @Override
      public void run() {
      this.doMakeup();
       }
      /**
       * 開始化妝
       */
      public void doMakeup(){
            if(flag == 0){
            synchronized (lipstick){
            System.out.println(this.girlName+" 拿著口紅");
            try {
            Thread.sleep(1000);
            } catch (InterruptedException e) {
            e.printStackTrace();
            }
            synchronized (mirror){
                System.out.println(this.girlName+" 拿著鏡子");
            }
     }
}else{
      synchronized (mirror){
            System.out.println(this.girlName+" 拿著鏡子");
            try {
              Thread.sleep(2000);
             } catch (InterruptedException e) {
              e.printStackTrace();
             }
            synchronized (lipstick){
              System.out.println(this.girlName+" 拿著口紅");
             }
           }
         }
    }
}

public class DeadLockThread {
  public static void main(String[] args) {
    new Makeup(0,"大丫").start();
    new Makeup(1,"小丫").start();
   }
}

上市代碼會出現一直運行無法結束的狀態,解決方法即是不能做synchronized嵌套,改動如下:

注意synchronized的位置變化:

/**
 * 口紅類
 */
class Lipstick{

}


/**
 * 鏡子類
 */
class Mirror{

}


/**
 * 化妝線程類
 */
class Makeup extends Thread{
    private int flag; //flag=0:拿著口紅。flag!=0:拿著鏡子
    private String girlName;
    static Lipstick lipstick = new Lipstick();
    static Mirror mirror = new Mirror();


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

    @Override
    public void run() {
        this.doMakeup();
    }
    /**
     * 開始化妝
     */
    public void doMakeup(){
        if(flag == 0){
            synchronized (lipstick){
                System.out.println(this.girlName+" 拿著口紅");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            synchronized (mirror){
                System.out.println(this.girlName+" 拿著鏡子");
            }
        }else{
            synchronized (mirror){
                System.out.println(this.girlName+" 拿著鏡子");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            synchronized (lipstick){
                System.out.println(this.girlName+" 拿著口紅");
            }
        }
    }
}

public class DeadLockThread {
    public static void main(String[] args) {
        new Makeup(0,"大丫").start();
        new Makeup(1,"小丫").start();
    }
}

經過修改后代碼正常運行。

原文鏈接:https://blog.csdn.net/m0_73944607/article/details/130682032

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新