使用jstack檢測Java應用的死鎖(deadlock)狀態
We can get the concept of deadlock in wikipedia.
The picture below gives a common scenario which leads to deadlock.
In this blog, I will share how to detect deadlock situation using JDK standard tool jstack. First we have to write a Java program which will lead to Deadlock:
package thread;public class DeadLockExample {/* * Thread 1: locked resource 1 Thread 2: locked resource 2 */public static void main(String[] args) {
final String resource1 = "ABAP";
final String resource2 = "Java";
// t1 tries to lock resource1 then resource2
Thread t1 = new Thread() {
public void run() {
synchronized (resource1) {
System.out.println("Thread 1: locked resource 1");
try {
Thread.sleep(100);
} catch (Exception e) {
}
synchronized (resource2) {
System.out.println("Thread 1: locked resource 2");
}
}
}
};
Thread t2 = new Thread() {
public void run() {
synchronized (resource2) {
System.out.println("Thread 2: locked resource 2");
try {
Thread.sleep(100);
} catch (Exception e) {
}
synchronized (resource1) {
System.out.println("Thread 2: locked resource 1");
}
}
}
};
t1.start();
t2.start();}}
Execute this program, you will get output: Thread 1: locked resource 1 Thread 2: locked resource 2 Then use command jps -l -m to list the process id of this deadlock program. In my example it is 51476:
Just type jstack + process id, and it will display all detailed information about deadlock:
Here the object 0x00000000d6f64988 and 0x00000000d6f649b8represent the two resource String “ABAP” and “Java”.
Update on 2017-03-04 Saturday 10:35PM
how to get the thread state of a long-running application
Suppose you have found a long-running application which has high CPU utilization rate and you would like to know which exactly line is relevant. Use the following code to simulate the long running situation:
package thread;import java.util.ArrayList;import java.util.List;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;class MyThread implements Runnable{
private List<Integer> myList;
private Object host;
public MyThread(List<Integer> list, Object object){
this.myList = list;
this.host = object;
}
public void updateListSafe(){
synchronized(this.host){
ArrayList<Integer> safe = new ArrayList<Integer>();
safe.add(1);
}
}
private void updateList(int i){
synchronized(this.host){
myList.add(i);
}
}
@Override
public void run() {
while(true){
updateList(1);
}
}}public class MyExecutor {
private ArrayList<Integer> taskList = new ArrayList<Integer>();
private Object object = new Object();
private void launch(){
ExecutorService executorService= Executors.newFixedThreadPool(10);
executorService.execute(new MyThread(taskList, object));
executorService.execute(new MyThread(taskList, object));
}
public static void main(String[] args) {
MyExecutor test = new MyExecutor();
test.launch();
}}
first find the process id of running application:
then use command jstack 23520 to get the stack trace:
In line 9 and line 18 our application method MyThread.updateList is listed there.
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2713514/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [Java]一個DeadLock(死鎖)的例子Java
- 死鎖_DeadLock_示例
- ORACLE死鎖檢測Oracle
- 【DEADLOCK】Oracle“死鎖”模擬Oracle
- 使用jstack檢視當前程序全部執行緒的狀態JS執行緒
- Java應用異常狀態監測Java
- mysql行鎖和死鎖檢測MySql
- 死鎖檢測及解決
- 如何使用jstack分析執行緒狀態JS執行緒
- 如何使用 jstack 分析執行緒狀態JS執行緒
- 測試庫死鎖診斷DEADLOCK DETECTED ( ORA-00060 )
- 淺談 Linux 的死鎖檢測Linux
- LiteOS:SpinLock自旋鎖及LockDep死鎖檢測
- oracle deadlock死鎖trace file分析之一Oracle
- RDSSQLServer死鎖(Deadlock)系列之三自動部署Profiler捕獲死鎖SQLServer
- 使用JDK自帶的工具jstack找出造成執行程式死鎖的原因JDKJS行程
- Java 死鎖(DeadLock)例項分析和預防[base jdk8]JavaJDK
- 使用oracle 10704 event分析獲取鎖lock及死鎖deadlock系列九Oracle
- python requests檢測響應狀態碼Python
- java檢測當前CPU負載狀態的方法Java負載
- Java鎖——死鎖Java
- RAC全域性死鎖檢測時間
- 作業系統(5) 死鎖的概念 死鎖產生的必要條件 死鎖的處理策略 預防死鎖 避免死鎖 死鎖的檢測和解除 銀行家演算法作業系統演算法
- 檢測網路狀態
- oracle lock轉換及oracle deadlock死鎖系列一Oracle
- oracle deadlock死鎖trace file分析之一增補Oracle
- jstack判斷執行緒狀態JS執行緒
- 檢測mysql狀態的指令碼MySql指令碼
- 關於 SAP HANA 資料庫的死鎖問題(deadlock)資料庫
- java執行緒的狀態+鎖分析Java執行緒
- Linux 死鎖檢測模組 Lockdep 簡介Linux
- golang 執行時死鎖排查和檢測Golang
- Java 中的死鎖Java
- oracle 死鎖發生的測試用例Oracle
- 檢視oracle死鎖程式並結束死鎖Oracle
- 檢測網路狀態 - flutterFlutter
- iOS 檢測網路狀態iOS
- 【死鎖】ORA-00060: deadlock detected while waiting for resourceWhileAI