1. 程式人生 > 其它 >(嚴格)次小生成樹

(嚴格)次小生成樹

死鎖

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

//死鎖:多個執行緒互相抱著對方需要的資源,然後形成僵持
public class DeadLock {
    public static void main(String[] args) {
        Makeup g1 = new Makeup(0,"小美");
        Makeup g2 = new Makeup(1,"小紅");
        g1.start();
        g2.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(1000);
                synchronized (lipstick) {//一秒鐘後獲得口紅
                    System.out.println(this.girlname + "獲得口紅的鎖");
                }
            }
        }
    }
}

死鎖避免的方法

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

    1. 互斥條件:一個資源每次只能被一個程序使用
    2. 請求與保持條件:一個程序因請求資源而阻塞時,對以獲得的資源保持不放
    3. 不剝奪條件:程序以獲得的資源,在未使用完之前,不能強行剝奪
    4. 迴圈等待條件:若干程序之間形成一種頭尾相接的迴圈等待資源關係

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

//化妝
    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(1000);
            }
            synchronized (lipstick) {//一秒鐘後獲得口紅
                System.out.println(this.girlname + "獲得口紅的鎖");
            }
        }
    }
}