编程语言
首页 > 编程语言> > java死锁

java死锁

作者:互联网

死锁

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

形成死锁的四个必要条件:

避免死锁:破坏形成死锁的四个必要条件其中任意一个就可以避免死锁的产生

package com.yuanyu.thread;

//死锁:多个对象持有对方需要的资源 相互僵持
public class DeadLock {
    public static void main(String[] args) {
        Makeup g1=new Makeup("xiaoming",0);
        Makeup g2=new Makeup("xiaohong",1);

        g1.start();
        g2.start();

    }
}

class LipStick{

}

class Mirror{

}

class Makeup extends Thread{
    String name;
    int choice;

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

    //需要的资源只有一份,用static来保证资源的唯一
    static LipStick lipStick =new LipStick();
    static Mirror mirror =new Mirror();

    @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+"获得了口红的锁");
                }
            }
            
        }
        }
    }

程序形成死锁:

image

package com.yuanyu.thread;

//死锁:多个对象持有对方需要的资源 相互僵持
public class DeadLock {
    public static void main(String[] args) {
        Makeup g1=new Makeup("xiaoming",0);
        Makeup g2=new Makeup("xiaohong",1);

        g1.start();
        g2.start();

    }
}

class LipStick{

}

class Mirror{

}

class Makeup extends Thread{
    String name;
    int choice;

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

    //需要的资源只有一份,用static来保证资源的唯一
    static LipStick lipStick =new LipStick();
    static Mirror mirror =new Mirror();

    @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+"获得了口红的锁");
            }
        }
        }
    }

程序运行结果:死锁问题解决

image

标签:java,name,Makeup,System,choice,死锁,static
来源: https://www.cnblogs.com/yuanyu610/p/15806832.html