使用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.
- Netty-整合Protobuf高性能数据传输
- Netty-整合kryo高性能数据传输
- 40个重要的HTML 5面试问题及答案
- js调用原生API--陀螺仪和加速器
- OpenDaylight开发-DataStoreChange监听器三种类型
- express模拟接口
- spring boot开发的日志系统
- elasticsearch 5.0.1安装analysis-ik分词器
- Spring Cloud中Feign如何统一设置验证token
- laravel+react+webpack+babel+gulp的配置
- OpenvSwitch系列之浅析main函数
- 没有公式如何看懂EM算法?
- Google用来处理海量文本去重的simhash算法原理及实现
- Open vSwitch系列之openflow版本兼容
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- C++职责链模式
- Newbe.Claptrap 框架入门,第四步 —— 利用 Minion,商品下单
- 使用 NextCloud + COS 搭建个人网盘
- Shader基础技巧整理
- 隐秘的物理粒子系统与渲染 !Cocos Creator LiquidFun !
- 一起用Gradle Transform API + ASM完成代码织入呀~
- 掩盖浏览痕迹不会被发现的方法【逗】
- webpack实战——样式预处理
- 前端防止浏览器访问旧版 js 和 css 的优化思路
- 少数派实测报告:AI巨无霸模型GPT-3 | 附送API 调用方法「AI核心算法」
- Qt音视频开发19-海康sdk录像存储
- 单元测试在深度学习中的应用 | 附代码「AI产品/工程落地」
- 实现 AOP 的三种方式
- Spring Boot 配置
- 彻底弄懂TCP协议:从三次握手说起