其他分享
首页 > 其他分享> > 死锁

死锁

作者:互联网

死锁
package com.lei.study03;

//死锁:多个线程互相拥有者对方需要的线程,然后形成僵持
public class DeadLock {
    public static void main(String[] args) {
        Makeup g1=new Makeup(0,"A");
        Makeup g2=new Makeup(1,"B");

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

//口红
class Lipstick{

}

//镜子
class Mirror{

}

//化妆
class Makeup extends Thread{

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

    int choice;//选择
    String girlName;//使用化妆品的人

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

    @Override
    public void run() {
        //化妆
        makeup();

    }

    //化妆,互相持有对方需要的资源
    private void makeup() {
        if(choice==0){
            synchronized (lipstick){//获得口红的锁
                System.out.println(this.girlName + "获得口红的锁");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                synchronized (mirror){//一秒后想获得镜子
                    System.out.println(this.girlName+"获得镜子的锁");
                 }
             }
        }else {
            synchronized (mirror){//获得口红的锁
                System.out.println(this.girlName+"获得镜子的锁");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                synchronized (lipstick){//一秒后想获得镜子
                    System.out.println(this.girlName + "获得口红的锁");
                }
            }
        }
}}
查看代码

package com.lei.study03;

//死锁:多个线程互相拥有者对方需要的线程,然后形成僵持
public class DeadLock {
    public static void main(String[] args) {
        Makeup g1=new Makeup(0,"A");
        Makeup g2=new Makeup(1,"B");

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

//口红
class Lipstick{

}

//镜子
class Mirror{

}

//化妆
class Makeup extends Thread{

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

    int choice;//选择
    String girlName;//使用化妆品的人

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

    @Override
    public void run() {
        //化妆
        makeup();

    }

    //化妆,互相持有对方需要的资源
    private void makeup() {
        if(choice==0){
            synchronized (lipstick){//获得口红的锁
                System.out.println(this.girlName + "获得口红的锁");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                
             }
             synchronized (mirror){//一秒后想获得镜子
                System.out.println(this.girlName+"获得镜子的锁");
            }
        }else {
            synchronized (mirror){//获得口红的锁
                System.out.println(this.girlName+"获得镜子的锁");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                
            }
            synchronized (lipstick){//一秒后想获得镜子
                System.out.println(this.girlName + "获得口红的锁");
            }
        }
}}

因为改了代码之后,原来的意思变了。原本是:拿着镜子用口红。现在是拿镜子,放回去,再拿口红。

标签:girlName,synchronized,镜子,口红,Makeup,choice,死锁
来源: https://www.cnblogs.com/evelei/p/16141571.html