1.死锁如何产生
说句难听话,锁一般都很少用到,何况死锁呢?想产生死锁还是有点难的,需要满足2个条件:
共享资源同时只能被一个线程使用,如果已经有一个线程占用了资源,其余线程只能等待,直到资源被释放。
死锁情况肯定存在多个资源被多个线程争抢的情况。
比如线程1持有了资源A,然后去等待获取资源B,线程2持有了资源B,然后等待获取资源A,这样就会形成死锁。
2.如何避免死锁
一般有2种方式避免死锁:
- 线程一次性获取需要的全部资源。
- 获取锁时,增加超时动作。如果在一定的时间内获取不到锁,则释放已经获取的锁。
3.代码实践
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、jstack等命令查看JVM线程栈和堆内存的情况。一般出现死锁时,会在线程栈的信息里出现deadlock字样。
还可以采用VisualVM、JConsole等工具进行排查。