一般在Java项目里用到锁的场景不多,有朋友调侃说用到锁的次数还没有面试被问到的次数多,哈哈!

1、死锁如何产生

说句难听话,锁一般都很少用到,何况死锁呢?想产生死锁还是有点难的,需要满足2个条件:

比如线程1持有了资源A,然后去等待获取资源B,线程2持有了资源B,然后等待获取资源A,这样就会形成死锁。

2、如何避免死锁

一般有2种方式避免死锁:

3、代码实践

/**
 * 避免死锁,我觉得有2种方式:
 * 1、线程直接一把头获取所需要的全部锁,不要分步
 * 2、线程获取A之后,再去获取B,超时仍未获取到B,则释放A
 */
public class AvoidDeadLock01 {
    private static Lock lock1 = new ReentrantLock();
    private static Lock lock2 = new ReentrantLock();
    public static void acquireLocks(Lock lock1, Lock lock2) {
        boolean isLock1Acquired = false;
        boolean isLock2Acquired = false;
        while (true) {
            try {
                isLock1Acquired = lock1.tryLock();
                isLock2Acquired = lock2.tryLock();
            } finally {
                if (isLock1Acquired && isLock2Acquired) {
                    return;
                }
                if (isLock1Acquired) {
                    lock1.unlock();
                }
                if (isLock2Acquired) {
                    lock2.unlock();
                }
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public static void main(String[] args) {
        Thread thread1 = new Thread(() -> {
            acquireLocks(lock1, lock2);
            System.out.println("=====线程1 获取到了2把锁=====");
            lock1.unlock();
            lock2.unlock();
        });
        Thread thread2 = new Thread(() -> {
            acquireLocks(lock1, lock2);
            System.out.println("=====线程2 获取到了2把锁=====");
            lock1.unlock();
            lock2.unlock();
        });
        thread1.start();
        thread2.start();
    }
}

public class AvoidDeadLock02 {
    private static Lock lock1 = new ReentrantLock();
    private static Lock lock2 = new ReentrantLock();
    public static void acquireLocks(Lock lock1, Lock lock2) {
        boolean isLock1Acquired = false;
        boolean isLock2Acquired = false;
        try {
            while (true) {
                isLock1Acquired = lock1.tryLock(200, TimeUnit.MILLISECONDS);
                if (isLock1Acquired) {
                    isLock2Acquired = lock2.tryLock(200, TimeUnit.MILLISECONDS);
                    if (isLock2Acquired) {
                        break;
                    } else {
                        lock1.unlock();
                    }
                }
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } finally {
            if (!isLock1Acquired || !isLock2Acquired) {
                if (isLock1Acquired) {
                    lock1.unlock();
                }
                if (isLock2Acquired) {
                    lock2.unlock();
                }
            }
        }
    }
    public static void main(String[] args) {
        Thread thread1 = new Thread(() -> {
            acquireLocks(lock1, lock2);
            System.out.println("=====线程1 获取到了2把锁=====");
            lock1.unlock();
            lock2.unlock();
        });
        Thread thread2 = new Thread(() -> {
            acquireLocks(lock1, lock2);
            System.out.println("=====线程2 获取到了2把锁=====");
            lock1.unlock();
            lock2.unlock();
        });
        thread1.start();
        thread2.start();
    }
}

4、出现死锁如何排查

一般出现死锁时,可能会导致CPU、内存等资源消耗过高,导致系统性能下降。也可能导致应用无响应或者假死等等,所以要从多角度进行死锁的排查。

首先是用top、df、free等命令查看操作系统的基本情况。然后可以使用jmap、等命令查看JVM线程栈和堆内存的情况。一般出现死锁时,会在线程栈的信息里出现字样。

还可以采用、等工具进行排查。

线程死锁的条件_线程死锁_线程死锁解决方法

———END———
限 时 特 惠: 本站每日持续更新海量各大内部创业教程,永久会员只需109元,全站资源免费下载 点击查看详情
站 长 微 信: nanadh666

声明:1、本内容转载于网络,版权归原作者所有!2、本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。3、本内容若侵犯到你的版权利益,请联系我们,会尽快给予删除处理!