欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

08_死锁-产生与解决

程序员文章站 2022-04-17 16:06:18
...

死锁——产生与解决

1、死锁

多个线程各自占用一些资源,并且相互等待其他线程占用的资源才能进行,而导致两个或多个线程都在等待对方释放资源,所以度停止执行了。

产生的原因:

过多的同步可能造成死锁

某一个同步块同时拥有两个以上对象的锁时,就可能发生死锁问题

死锁出现的情况:

package com.tsymq.thread.concurrnet;

public class DeadLock {
    public static void main(String[] args) {
        TeamMember tm1 = new TeamMember(1, "五五开");
        TeamMember tm2 = new TeamMember(2, "蛇哥");
        tm1.start();
        tm2.start();
    }
}

class AWM{}

class Scope8{}

class TeamMember extends Thread {
    static AWM awm = new AWM();
    static Scope8 scope8 = new Scope8();
    private String name;
    private int component;

    public TeamMember(int component, String name) {
        super(name);
        this.name = name;
        this.component = component;
    }

    @Override
    public void run() {
        pickup();
    }

    public void pickup(){
        if (component == 1){
            synchronized (awm){
                System.out.println(this.getName() + "获得AWM!");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (scope8){
                    System.out.println(super.getName() + "获得8倍镜!");
                }
            }
        } else {
            synchronized (scope8){
                System.out.println(super.getName() + "获得8倍镜!");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (awm){
                    System.out.println(this.getName() + "获得AWM!");
                }
            }
        }
    }
}

AWM和8倍镜都只有1个,一开始五五开拿到AWM,蛇哥拿到8倍镜,1秒钟后,五五开还想要一个8倍镜,而此时8倍镜还在蛇哥手中,所以只能等待,又过了1秒钟,蛇哥还想要AWM,可是AWM还在开哥手中,也只能等待,两个人互不想让,形成了死锁

解决办法:

不要放在同一个代码块中,不要锁套锁,可避免死锁发生

package com.tsymq.thread.concurrnet;

public class DeadLock {
    public static void main(String[] args) {
        TeamMember tm1 = new TeamMember(1, "五五开");
        TeamMember tm2 = new TeamMember(2, "蛇哥");
        tm1.start();
        tm2.start();
    }
}

class AWM{}

class Scope8{}

class TeamMember extends Thread {
    static AWM awm = new AWM();
    static Scope8 scope8 = new Scope8();
    private String name;
    private int component;

    public TeamMember(int component, String name) {
        super(name);
        this.name = name;
        this.component = component;
    }

    @Override
    public void run() {
        pickup();
    }

    public void pickup(){
        if (component == 1){
            synchronized (awm){
                System.out.println(this.getName() + "获得AWM!");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // synchronized (scope8){
                //     System.out.println(super.getName() + "获得8倍镜!");
                // }
            }
            synchronized (scope8){
                System.out.println(super.getName() + "获得8倍镜!");
            }
        } else {
            synchronized (scope8){
                System.out.println(super.getName() + "获得8倍镜!");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // synchronized (awm){
                //     System.out.println(this.getName() + "获得AWM!");
                // }
            }
            synchronized (awm){
                System.out.println(this.getName() + "获得AWM!");
            }
        }
    }
}

在五五开拿到AWM,蛇哥拿到8倍镜后,1秒钟后,开哥放弃了AWM,想要8倍镜,但是还在蛇哥手中,所以等待,又1秒钟后,蛇哥放弃了8倍镜,想要AWM,此时开哥已经放弃了AWM,蛇哥拿到了AWM,由于蛇哥已经放弃了8倍镜,所以开哥成功拿到8倍镜