1.jstack
可以分析java进程中,线程死锁/线程问题
案例1死锁
`@ApiOperation("登录")
@PostMapping("/login")
@ResponseBody
public CommonResult
@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
获取堆,非堆内存,永久代信息