文章目錄
- 多執行緒的死鎖
- 使用JDK檢測死鎖
多執行緒的死鎖
- Java執行緒死鎖是一個經典的多執行緒問題, 因為不同的執行緒都在等待根本不可能被釋放的鎖, 從而導致執行緒中的任務無法完成
- 下面演示互相等待對方鎖釋放的死鎖
/**
* Created with IntelliJ IDEA.
* Description: If you don't work hard, you will a loser.
* User: Listen-Y.
* Date: 2020-10-03
* Time: 20:37
*/
public class DealThread implements Runnable {
private String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
//倆把鎖
private final Object lock1 = new Object();
private final Object lock2 = new Object();
@Override
public void run() {
if ("a".equals(userName)) {
synchronized (lock1) {
System.out.println("userName=" + userName + " :" + "進入lock1");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock2) {
System.out.println("由lock1->進入->lock2");
}
}
}
if ("b".equals(userName)) {
synchronized (lock2) {
System.out.println("userName=" + userName + " :" + "進入lock2");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock1) {
System.out.println("由lock2->進入->lock1");
}
}
}
}
}
- 啟動類
/**
* Created with IntelliJ IDEA.
* Description: If you don't work hard, you will a loser.
* User: Listen-Y.
* Date: 2020-10-03
* Time: 20:42
*/
public class Run {
public static void main(String[] args) throws InterruptedException {
DealThread dealThread = new DealThread();
dealThread.setUserName("a");
Thread thread = new Thread(dealThread);
thread.start();
//注意這里是等待0.1秒 而執行緒是等待三秒
Thread.sleep(100);
dealThread.setUserName("b");
Thread thread1 = new Thread(dealThread);
thread1.start();
}
}
- 運行結果

- 此時我們發現已經進入死鎖
使用JDK檢測死鎖
- 在你安裝JDK的bin目錄下按住shift加滑鼠右鍵啟動powershell
- 輸入jps命令

- 知道我們啟動類中死鎖的執行緒的執行緒id是14784
- 使用jstack命令

- 在最后面獲得資訊

- 瀏覽資訊知道出現死鎖的代碼行在哪, 和在等待什么鎖導致的死鎖都可以發現.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/155173.html
標籤:其他
上一篇:ACM-ICPC 2017 Asia HongKong I題 Count the Even Integers【遞回公式+記憶化】
