9、死锁

package com.example.demo.thread;

/**
 * @projectName: demo
 * @package: com.example.demo.thread
 * @className: TestDeath
 * @author: 
 * @description: 测试死锁:多个线程互相拥有相同的资源会出现死锁现象
 * @date: 2021/12/8 14:56
 */
public class TestDeath {
    public static void main(String[] args) {
        MakeUp zmakeUp = new MakeUp(1, "张三");
        MakeUp lmakeUp = new MakeUp(0, "李三");
        new Thread(zmakeUp).start();
        new Thread(lmakeUp).start();
    }
}

class MakeUp implements Runnable {
    // static在创建对象前初始化,保证唯一性
    static Lipstick lipstick = new Lipstick();
    static Mirror mirror = new Mirror();

    int choose;
    String personName;

    public MakeUp(int choose, String personName) {
        this.choose = choose;
        this.personName = personName;
    }


    @Override
    public void run() {
        if (choose == 0) {
            synchronized (lipstick) {
                System.out.println(personName + ":先使用口红化妆");
                System.out.println("口红哈希" + lipstick.hashCode());
                try {
                    Thread.sleep(1000);
                    synchronized (mirror) {
                        System.out.println(personName + ":再使用镜子化妆");
                        System.out.println("镜子哈希" + mirror.hashCode());
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } else {
            synchronized (mirror) {
                System.out.println(personName + ":再使用镜子化妆");
                System.out.println("镜子哈希" + mirror.hashCode());
                try {
                    Thread.sleep(1000);
                    synchronized (lipstick) {
                        System.out.println(personName + ":先使用口红化妆");
                        System.out.println("口红哈希" + lipstick.hashCode());
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

class Lipstick {

}

class Mirror {

}
资源不放

上面样例解决方案

synchronized不要嵌套使用

package com.example.demo.thread;

/**
 * @projectName: demo
 * @package: com.example.demo.thread
 * @className: TestDeath
 * @author:
 * @description: 测试死锁:synchronized块中不要出现另一个synchronized块
 * @date: 2021/12/8 14:56
 */
public class TestDeath {
    public static void main(String[] args) {
        MakeUp zmakeUp = new MakeUp(1, "张三");
        MakeUp lmakeUp = new MakeUp(0, "李三");
        new Thread(zmakeUp).start();
        new Thread(lmakeUp).start();
    }
}

class MakeUp implements Runnable {
    // static在创建对象前初始化,保证唯一性
    static Lipstick lipstick = new Lipstick();
    static Mirror mirror = new Mirror();

    int choose;
    String personName;

    public MakeUp(int choose, String personName) {
        this.choose = choose;
        this.personName = personName;
    }


    @Override
    public void run() {
        if (choose == 0) {
            synchronized (lipstick) {
                System.out.println(personName + ":先使用口红化妆");
                System.out.println("口红哈希" + lipstick.hashCode());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (mirror) {
                    System.out.println(personName + ":再使用镜子化妆");
                    System.out.println("镜子哈希" + mirror.hashCode());
                }
            }
        } else {
            synchronized (mirror) {
                System.out.println(personName + ":再使用镜子化妆");
                System.out.println("镜子哈希" + mirror.hashCode());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            synchronized (lipstick) {
                System.out.println(personName + ":先使用口红化妆");
                System.out.println("口红哈希" + lipstick.hashCode());
            }
        }
    }
}

class Lipstick {

}

class Mirror {

}
image.png

你可能感兴趣的:(9、死锁)