死锁

image
image

死锁

package com.guo.thread;

//死锁:多个线程互相抱着对象需要的资源,让后形成僵持
public class DeadLock {
    public static void main(String[] args) {
        Makeup liyue = new Makeup(0, "李悦");
        Makeup chenguo = new Makeup(1, "陈果");

        liyue.start();
        chenguo.start();

    }
}


//口红
class Lipstick{

}

//镜子
class Mirror{

}


class Makeup extends Thread{

    //需要的资源只有一份。用static来保证只有一份
    static Lipstick lipstick = new Lipstick();
    static Mirror mirror= new Mirror();

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

    public Makeup(int choice,String girlName) {

        this.choice = choice;
        this.girlName = girlName;
    }

    @Override
    public void run() {
        //化妆
        try {
            makeup();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    //互相持有对方的所,就是需要拿到对方的资源
    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 + "获得口红的锁");
                }
            }//退出这个代码块,才会释放镜子锁

            

        }

    }

}

image

修改后

package com.guo.thread;

//死锁:多个线程互相抱着对象需要的资源,让后形成僵持
public class DeadLock {
    public static void main(String[] args) {
        Makeup liyue = new Makeup(0, "李悦");
        Makeup chenguo = new Makeup(1, "陈果");

        liyue.start();
        chenguo.start();

    }
}


//口红
class Lipstick{

}

//镜子
class Mirror{

}


class Makeup extends Thread{

    //需要的资源只有一份。用static来保证只有一份
    static Lipstick lipstick = new Lipstick();
    static Mirror mirror= new Mirror();

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

    public Makeup(int choice,String girlName) {

        this.choice = choice;
        this.girlName = girlName;
    }

    @Override
    public void run() {
        //化妆
        try {
            makeup();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    //互相持有对方的所,就是需要拿到对方的资源
    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 + "获得口红的锁");
            }

        }

    }

}

image

posted @ 2026-03-27 16:42  果子同志  阅读(5)  评论(0)    收藏  举报