JVM問題定位工具

辉辉、發表於2024-08-07

1.jstack
可以分析java程序中,執行緒死鎖/執行緒問題
案例1死鎖
`@ApiOperation("登入")
@PostMapping("/login")
@ResponseBody
public CommonResult login(
@RequestBody UmsAdminLoginParam umsAdminLoginParam
) {
new Thread(() -> {
synchronized (lockA) {
try {
System.out.println("執行緒1開始執行");
Thread.sleep(2000);
} catch (InterruptedException e) {
}
synchronized (lockB) {
System.out.println("執行緒1執行結束
");
}
}
}).start();

    new Thread(() -> {
        synchronized (lockB) {
            try {
                System.out.println("執行緒2開始執行========");
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
            synchronized (lockA) {
                System.out.println("執行緒2結束執行========");
            }
        }
    }).start();

    System.out.println("主執行緒執行結束========");

// try {
// String token = adminService.login(umsAdminLoginParam.getUsername(),umsAdminLoginParam.getPassword());
// return CommonResult.success(token);
// } catch (Exception e) {
//
// }
return CommonResult.failed("執行死鎖程式碼");
}`
執行jstack -l pid即可檢視死鎖程式碼相關資訊
Java stack information for the threads listed above:

"Thread-71":
at com.mall.kaop.controller.UmsAdminController.lambda$login$1(UmsAdminController.java:85)
- waiting to lock <0x0000000732999d80> (a java.lang.Object)
- locked <0x0000000732999d70> (a java.lang.Object)
at com.mall.kaop.controller.UmsAdminController$$Lambda$704/1619667033.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)
"Thread-70":
at com.mall.kaop.controller.UmsAdminController.lambda$login$0(UmsAdminController.java:72)
- waiting to lock <0x0000000732999d70> (a java.lang.Object)
- locked <0x0000000732999d80> (a java.lang.Object)
at com.mall.kaop.controller.UmsAdminController$$Lambda$703/1177482355.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)

Found 1 deadlock.
2.jmap
獲取堆,非堆記憶體,永久代資訊

相關文章