死锁

105 阅读2分钟
  1. “死锁”问题:多个线程各自占有一些共享资源,并且互相等待其他线程占有的资源才能运行,而导致两个或者多个线程都在等待对方释放资源,都停止执行的情形。某个同步块同时拥有**“两个以上对象的锁”**时,就可能会发生“死锁”问题。
  2. 产生死锁的四个必要条件:
  • 互斥条件:一个资源每次只能被一个进程使用 下面这个例子就是这种情况
  • 请求与保持条件:一个进程因请求资源而阻塞时,对已获得的资源保持不放
  • 不剥夺条件:进程已获得的资源,在未使用完之前,不能强行剥夺
  • 循环等待条件:若干进程之间形成了一种头尾相接的循环等待资源关系。
public class deadLock {

    public static void main(String[] args) {
        makeup xiaohong = new makeup(0, "xiaohong");
        makeup xiaolan = new makeup(1, "xiaolan");

        xiaohong.start();
        xiaolan.start();
    }

}

//口红
class Lipstick{

}

//镜子
class Mirror{

}

//化妆
class makeup extends Thread{

    //需要的资源只有一份
    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() {
        //化妆的方法
        make();
    }

    protected void make(){
        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+"获得镜子");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }else {
            synchronized (mirror){
                System.out.println(this.name+"获得镜子");
                synchronized (lipstick){
                    System.out.println(this.name+"获得口红");
                }
            }
        }
    }


}

输出结果:(发生死锁,线程停不下来)

  1. 解决方法:只要将发生死锁的四个必要条件中任意一个条件给破解,就可以避免死锁的发生。
package com.Thread.Lock;


// --> 解决死锁:不要将两个进程放在一起,即不要让两个人同时抱同一把锁


public class deadLockSolve {

    public static void main(String[] args) {
        makeup1 xiaohong = new makeup1(0, "xiaohong");
        makeup1 xiaolan = new makeup1(1, "xiaolan");

        xiaohong.start();
        xiaolan.start();
    }

}

//口红
class Lipstick1 {

}

//镜子
class Mirror1 {

}

//化妆
class makeup1 extends Thread {

    //需要的资源只有一份
    static Lipstick1 lipstick = new Lipstick1();
    static Mirror1 mirror = new Mirror1();

    int choice;
    String name;

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

    @Override
    public void run() {
        //化妆的方法
        make();
    }

    private void make() {
        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(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

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


}

输出结果: