多线程——死锁( 理解 )

在这里插入图片描述

package thread;


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

// 产生死锁的四个必要条件:
//    1.互斥条件: 一个资源每次只能被一个进程使用。
//    2.请求与保持条件:一个进程因请求资源而阻塞时候,对已获得的资源保持不放。
//    3.不剥夺条件:进程已获得的资源,在未使用完之前,不能强行剥夺。
//    4.循环等条件:若干进程之间形成一种头尾相接的循环等待资源关系。
//
//   只要一个条件避开就不会导致死锁
public class Deadlock {
    public static void main(String[] args) {
        Makeup makeup1 = new Makeup(0," girl ");
        Makeup makeup2 = new Makeup(2," princess ");

        makeup1.start();
        makeup2.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(2000);

                synchronized (lipstick){
                    System.out.println(this.girlName+"获得口红的锁");
                }
            }
        }
    }
}

在这里插入图片描述

package thread;


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

// 产生死锁的四个必要条件:
//    1.互斥条件: 一个资源每次只能被一个进程使用。
//    2.请求与保持条件:一个进程因请求资源而阻塞时候,对已获得的资源保持不放。
//    3.不剥夺条件:进程已获得的资源,在未使用完之前,不能强行剥夺。
//    4.循环等条件:若干进程之间形成一种头尾相接的循环等待资源关系。
//
//   只要一个条件避开就不会导致死锁
public class Deadlock {
    public static void main(String[] args) {
        Makeup makeup1 = new Makeup(0," girl ");
        Makeup makeup2 = new Makeup(2," princess ");

        makeup1.start();
        makeup2.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(2000);

                }

            synchronized (lipstick){
                System.out.println(this.girlName+"获得口红的锁");
            }
        }
    }
}

https://www.bilibili.com/video/BV1V4411p7EF?p=22

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值