死锁

suaxi
2020-11-26 / 0 评论 / 56 阅读 / 正在检测是否收录...

死锁

定义:多个线程互相拥抱着对方需要的资源,然后形成僵持

产生死锁的四个必要条件:

1、互斥:一个资源每次只能被一个进程使用

2、请求与保持:一个进程因请求资源而阻塞时,对已获得的资源保持不放

3、不剥夺条件:进程已获得的资源,在未使用完之前,不能强行剥夺

4、循环等待:若干进程之间形成一种头尾相接的循环等待资源关系

死锁实例:

package com.thread.threadDemo;

/**
 * @Author suaxi
 * @Date 2020/11/26 16:18
 * 死锁:多个线程互相拥抱着对方需要的资源,然后形成僵持
 */
public class DeadLock {
    public static void main(String[] args) {
        MakeUp p1 = new MakeUp(0, "孙笑川");
        MakeUp p2 = new MakeUp(1, "药水哥");

        p1.start();
        p2.start();
    }
}

//口红
class Lipstick{

}

//镜子
class Mirror{

}

class MakeUp extends Thread{
    //需要的资源只有一份,用statick来保证只有一份
    static Lipstick lipstick = new Lipstick();
    static Mirror mirror = new Mirror();

    int choice; //选择
    String name; //使用人

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

    @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.name+"获得口红的锁");
                Thread.sleep(1000);
                synchronized (mirror){ //等待一秒后想拿镜子
                System.out.println(this.name+"获得镜子的锁");
            }
          }
        }else {
            synchronized (mirror){//拿到镜子
                System.out.println(this.name+"获得镜子的锁");
                Thread.sleep(2000);
                synchronized (lipstick) { //等待两秒后想拿口红
                System.out.println(this.name + "获得口红的锁");
                }
            }
        }
    }
}

DeadLock01.png

将choice==0中的synchronized (mirror)拿到synchronized (lipStick)之外,else中的代码同理,即可解决实例中的死锁问题

package com.thread.threadDemo;

/**
 * @Author suaxi
 * @Date 2020/11/26 16:18
 * 死锁:多个线程互相拥抱着对方需要的资源,然后形成僵持
 */
public class DeadLock {
    public static void main(String[] args) {
        MakeUp p1 = new MakeUp(0, "孙笑川");
        MakeUp p2 = new MakeUp(1, "药水哥");

        p1.start();
        p2.start();
    }
}

//口红
class Lipstick{

}

//镜子
class Mirror{

}

class MakeUp extends Thread{
    //需要的资源只有一份,用statick来保证只有一份
    static Lipstick lipstick = new Lipstick();
    static Mirror mirror = new Mirror();

    int choice; //选择
    String name; //使用人

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

    @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.name+"获得口红的锁");
                Thread.sleep(1000);
            }
            synchronized (mirror){ //等待一秒后想拿镜子
                System.out.println(this.name+"获得镜子的锁");
            }
        }else {
            synchronized (mirror){//拿到镜子
                System.out.println(this.name+"获得镜子的锁");
                Thread.sleep(2000);
            }
            synchronized (lipstick) { //等待两秒后想拿口红
                System.out.println(this.name + "获得口红的锁");
            }
        }
    }
}

DeadLock02.png

0

评论 (0)

取消