线程死锁的原因&如何避免死锁

线程死锁的现象

两个或者两个以上的线程在执行过程中,因资源争夺而造成的互相等待的现象,在无外力作用的情况下,这些线程会一直互相等待而无法继续运行下去。

线程死锁的四个条件

  • 互斥条件
    资源只能被一个线程占用,如果其他线程请求获取该资源,则请求者只能等待,直到占用资源的线程释放该资源
  • 请求并持有条件
    指一个线程已经持有了至少一个资源,但又提出了新的资源请求,而新的资源已被其他线程占用,所以当前线程会被阻塞,但阻塞的同时不释放自己获取的资源
  • 不可剥夺条件
    获取到的资源在自己使用完之前不能被其他线程抢占,只能在使用完之后释放
  • 环路等待条件
    发生死锁的时候必然存在一个线程-资源的环形链,即线程集合{T0,T1,T2,…Tn}中的T0正在等待一个T1占用的资源,T1正在等待T2占用的资源,…Tn正在等待T1占用的资源

看一个例子:

public class DeadLockTest {
    //创建两个竞争的资源
    private static Object resourceA = new Object();
    private static Object resourceB = new Object();

    public static void main(String[] args) {
        //创建线程
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (resourceA){
                    System.out.println(Thread.currentThread()+"get ResourceA");
                    try {
                        Thread.sleep(1000);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread()+"waiting get ResourceB");
                    synchronized (resourceB){
                        System.out.println(Thread.currentThread()+"get ResourceB");
                    }
                }
            }
        });
        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (resourceB){
                    System.out.println(Thread.currentThread()+"get ResourceB");
                    try {
                        Thread.sleep(1000);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread()+"waiting get ResourceA");
                    synchronized (resourceA){
                        System.out.println(Thread.currentThread()+"get ResourceA");
                    }
                }
            }
        });
        //启动线程
        threadA.start();
        threadB.start();
    }
}

运行结果:

Thread[Thread-0,5,main]get ResourceA
Thread[Thread-1,5,main]get ResourceB
Thread[Thread-0,5,main]waiting get ResourceB
Thread[Thread-1,5,main]waiting get ResourceA

线程A持有资源A在等待资源B,线程B持有资源B在等待资源A,造成了死锁。

避免死锁的方法

在造成死锁的四个条件中:只有请求与保持条件环路等待条件这两个是可以破坏的。

资源申请的有序性是可以破坏资源的请求并持有条件和环路等待条件。


修改上面的例子:

public class DeadLockTest {
    //创建两个竞争的资源
    private static Object resourceA = new Object();
    private static Object resourceB = new Object();

    public static void main(String[] args) {
        //创建线程
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (resourceA){
                    System.out.println(Thread.currentThread()+"get ResourceA");
                    try {
                        Thread.sleep(1000);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread()+"waiting get ResourceB");
                    synchronized (resourceB){
                        System.out.println(Thread.currentThread()+"get ResourceB");
                    }
                }
            }
        });
        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (resourceA){
                    System.out.println(Thread.currentThread()+"get ResourceA");
                    try {
                        Thread.sleep(1000);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread()+"waiting get ResourceB");
                    synchronized (resourceB){
                        System.out.println(Thread.currentThread()+"get ResourceB");
                    }
                }
            }
        });
        //启动线程
        threadA.start();
        threadB.start();
    }
}

程序输出:

Thread[Thread-1,5,main]get ResourceA
Thread[Thread-1,5,main]waiting get ResourceB
Thread[Thread-1,5,main]get ResourceB
Thread[Thread-0,5,main]get ResourceA
Thread[Thread-0,5,main]waiting get ResourceB
Thread[Thread-0,5,main]get ResourceB

解决了死锁问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值