死锁案例

死锁

  • 多个线程各自占有一些共享资源,并且互相等待其他线程占有的资源才能运行,而导致两个或者多个线程都在等待对方释放资源,都停止执行的情形,某一个同步块同时拥有 "两个以上对象的锁"时,就可能会发生 “死锁” 的问题。

死锁避免方法

  • 产生死锁的四个必要条件:
    • 互斥条件:一个资源每次只能被一个进程使用。
    • 请求与保持条件:一个进程因请求资源而阻塞时,对已获得的资源保持不放。
    • 不剥夺条件:进程已获得的资源,在未使用完之前,不能强行剥夺。
    • 循环等待条件:若干进程之间形成一种头尾相接的循环等待资源关系。
以上列出了死锁的四个必要条件,我们只要想办法破其中的任意一个或多个条件就可以避免死锁的发生

案例

package Thread;

//死锁 :多个线程互相拿着对方所需要的资源,然后形成僵持
public class DeadLock {
    
    

    public static void main(String[] args) {
    
    

        Makeop makeup = new Makeop(0,"妹妹");
        Makeop makeup1 = new Makeop(1,"姐姐");

        makeup.start();
        makeup1.start();


    }
}


//口红
class Liostick{
    
    


}

//镜子
class Mirrir{
    
    

}

class Makeop extends  Thread {
    
    

    //需要的资源只有一份,用static来保证只有一份
    static Liostick lt = new Liostick();
    static Mirrir mr = new Mirrir();

    //选择
    int choice;
    //使用化妆品的人
    String girlName;

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

    public void run() {
    
    
        //化妆
        try {
    
    
            makeup();
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
    }

    private void makeup() throws InterruptedException {
    
    
        if (choice == 0) {
    
    
            //获得口红的锁
            synchronized (lt) {
    
    
                System.out.println(this.getName() + "获得口红的锁");
                // 1s 之后获得镜子
                Thread.sleep(1000);

                //获得镜子的锁
                synchronized (mr) {
    
    
                    System.out.println(this.getName() + "获得镜子的锁");
                }
            }
        } else {
    
    
            //获得镜子的锁
            synchronized (mr) {
    
    
                System.out.println(this.getName() + "获得镜子的锁");
                // 1s 之后获得口红
                Thread.sleep(1000);

                //获得口红的锁
                synchronized (lt) {
    
    
                    System.out.println(this.getName() + "获得口红的锁");
                }

            }
        }

    }
}

修改之后的案例: 把锁放在另一个锁外面

package Thread;

//死锁 :多个线程互相拿着对方所需要的资源,然后形成僵持
public class DeadLock {
    
    

    public static void main(String[] args) {
    
    

        Makeop makeup = new Makeop(0,"妹妹");
        Makeop makeup1 = new Makeop(1,"姐姐");

        makeup.start();
        makeup1.start();


    }
}


//口红
class Liostick{
    
    


}

//镜子
class Mirrir{
    
    

}

class Makeop extends  Thread {
    
    

    //需要的资源只有一份,用static来保证只有一份
    static Liostick lt = new Liostick();
    static Mirrir mr = new Mirrir();

    //选择
    int choice;
    //使用化妆品的人
    String girlName;

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

    public void run() {
    
    
        //化妆
        try {
    
    
            makeup();
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
    }

    private void makeup() throws InterruptedException {
    
    
        if (choice == 0) {
    
    
            //获得口红的锁
            synchronized (lt) {
    
    
                System.out.println(this.getName() + "获得口红的锁");
                // 1s 之后获得镜子
                Thread.sleep(1000);


                }
            //获得镜子的锁
            synchronized (mr) {
    
    
                System.out.println(this.getName() + "获得镜子的锁");
            }
        } else {
    
    
            //获得镜子的锁
            synchronized (mr) {
    
    
                System.out.println(this.getName() + "获得镜子的锁");
                // 1s 之后获得口红
                Thread.sleep(2000);


                }
            //获得口红的锁
            synchronized (lt) {
    
    
                System.out.println(this.getName() + "获得口红的锁");

            }
        }

    }
}

猜你喜欢

转载自blog.csdn.net/qq_45162683/article/details/111643678