使用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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 使用jstack檢視當前程序全部執行緒的狀態JS執行緒
- Java應用異常狀態監測Java
- 如何使用jstack分析執行緒狀態JS執行緒
- mysql行鎖和死鎖檢測MySql
- 死鎖避免中的安全狀態和不安全狀態
- 使用JDK自帶的工具jstack找出造成執行程式死鎖的原因JDKJS行程
- LiteOS:SpinLock自旋鎖及LockDep死鎖檢測
- python requests檢測響應狀態碼Python
- 關於 SAP HANA 資料庫的死鎖問題(deadlock)資料庫
- java檢測當前CPU負載狀態的方法Java負載
- jstack判斷執行緒狀態JS執行緒
- 作業系統(5) 死鎖的概念 死鎖產生的必要條件 死鎖的處理策略 預防死鎖 避免死鎖 死鎖的檢測和解除 銀行家演算法作業系統演算法
- java執行緒的狀態+鎖分析Java執行緒
- golang 執行時死鎖排查和檢測Golang
- Java 中的死鎖Java
- 檢視oracle死鎖程式並結束死鎖Oracle
- Java的jstack命令使用詳解JavaJS
- 檢測網路狀態 - flutterFlutter
- 使用 Provider 管理 Flutter 應用狀態 (下)IDEFlutter
- 使用 Provider 管理 Flutter 應用狀態 (上)IDEFlutter
- ORA-00060: Deadlock detected 模擬死鎖產生與解決方案
- ABAP面試題系列:寫一組會出現死鎖(Deadlock)的ABAP程式面試題
- 鎖的使用與死鎖的避免
- Java執行緒狀態及同步鎖Java執行緒
- 網路狀態檢測的利器 - ss命令
- mysql innodb_deadlock_detect檢測和處理MySql
- 例項詳解 Java 死鎖與破解死鎖Java
- 使用NAS動態儲存卷建立有狀態應用
- JAVA死鎖排查-效能測試問題排查思路Java
- mysql死鎖deadlock相關幾個系統變數innodb_lock_wait_timeoutMySql變數AI
- Linux中系統狀態檢測命令Linux
- 檢視使用 MySQL Shell 的連線狀態MySql
- 通過規則引擎跟蹤Java執行狀態的檢測工具:BytemanJava
- Java中常見死鎖與活鎖的例項Java
- java如何避免程式死鎖Java
- [Java併發]避免死鎖Java
- 使用Python和Java呼叫Shell指令碼時的死鎖陷阱PythonJava指令碼
- SQL SERVER死鎖查詢,死鎖分析,解鎖,查詢佔用SQLServer