Java多线程之死锁

本篇是学习死锁的知识,例子是化妆场景,只有一份镜子和口红,A先得到口红,再想要镜子;B先拿了镜子还想要口红;如果不先释放自己手中的物品的锁时,就会造成相互僵持的场景,造成死锁现象。 

package lesson04;

//死锁:化妆
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 name;
    //有参构造器

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

    @Override
    public void run() {
        makeup();
    }

    private void makeup() {
        if (choice == 0) {
            //只有在得到镜子的锁以后,才会释放口红的锁
            synchronized (lipstick){//拿到口红的锁
                System.out.println(this.name+"拿到了口红的锁");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (mirror){
                    System.out.println(this.name+"拿到了镜子的锁");//一秒后想要拿镜子的锁
                }
            }

        }else {
            //只有得到口红的锁以后,才会释放镜子的锁
            synchronized (mirror){
                System.out.println(this.name+"拿到了镜子的锁");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lipstick){
                    System.out.println(this.name+"拿到了口红的锁");
                }
            }

        }
    }

}

 

 运行结果:

package lesson04;

//死锁:化妆
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 name;
    //有参构造器

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

    @Override
    public void run() {
        makeup();
    }

    private void makeup() {
        if (choice == 0) {
            synchronized (lipstick){//拿到口红的锁
                System.out.println(this.name+"拿到了口红的锁");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            synchronized (mirror){
                System.out.println(this.name+"拿到了镜子的锁");//一秒后想要拿镜子的锁
            }
        }else {
            synchronized (mirror){
                System.out.println(this.name+"拿到了镜子的锁");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            synchronized (lipstick){
                System.out.println(this.name+"拿到了口红的锁");
            }
        }
    }

}

运行结果:

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值