当前位置:网站首页>JVM命令之 jstack:打印JVM中线程快照
JVM命令之 jstack:打印JVM中线程快照
2022-07-07 00:46:00 【张俊杰1994】
学习 尚硅谷 宋红康 JVM从入门到精通 的学习笔记
概述
jstack(JVM Stack Trace)是用于生成虚拟机指定进程当前时刻的线程快照(虚拟机堆栈跟踪),线程快照就是当前虚拟机内指定进程的每一条线程正在执行的方法堆栈的集合.
生成线程的快照的作用: 多线程在执行过程中可能会出现长时间停顿的问题,线程争抢资源的时候有的线程就需要等待同步监视器或者死锁,或者死循环,等等以上情况都会导致线程不正常的情况, 会出现长时间的停顿,要想知道是哪段代码导致的线程停顿的,这个时候就需要jstack指令了
语法
基本语法
option参数:-F
当正常输出的请求不被响应时,强制输出线程堆栈
option参数:-l
除堆栈外,显示关于锁的附加信息
option参数:-m
如果调用本地方法的话,可以显示C/C++的堆栈
option参数:-h
帮助操作
基本语法说明:
举例如下:
1.
2.加-l参数:
总结:
如果程序出现等待问题,可以使用该指令去查看问题所在,结果中也会提示你问题所在
演示
死锁问题排查
/** * 演示线程的死锁问题 */
public class ThreadDeadLock {
public static void main(String[] args) {
StringBuilder s1 = new StringBuilder();
StringBuilder s2 = new StringBuilder();
new Thread(){
@Override
public void run() {
synchronized (s1){
s1.append("a");
s2.append("1");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (s2){
s1.append("b");
s2.append("2");
System.out.println(s1);
System.out.println(s2);
}
}
}
}.start();
new Thread(new Runnable() {
@Override
public void run() {
synchronized (s2){
s1.append("c");
s2.append("3");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (s1){
s1.append("d");
s2.append("4");
System.out.println(s1);
System.out.println(s2);
}
}
}
}).start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(new Runnable() {
@Override
public void run() {
Map<Thread, StackTraceElement[]> all = Thread.getAllStackTraces();//追踪当前进程中的所有的线程
Set<Map.Entry<Thread, StackTraceElement[]>> entries = all.entrySet();
for(Map.Entry<Thread, StackTraceElement[]> en : entries){
Thread t = en.getKey();
StackTraceElement[] v = en.getValue();
System.out.println("【Thread name is :" + t.getName() + "】");
for(StackTraceElement s : v){
System.out.println("\t" + s.toString());
}
}
}
}).start();
}
}
说明,第一个线程先获取s1锁,再获取s2锁 , 第二个线程先获取s2锁,再获取s1锁,这样就很容易出现死锁了
用命令排查
启动上面的代码
这是就打印了线程的相关信息了,
箭头标识的这两个线程出现了阻塞状态,就是因为死锁了,
然后里面就显示deadlock了.
线程睡眠问题排查
public class TreadSleepTest {
public static void main(String[] args) {
System.out.println("hello - 1");
try {
Thread.sleep(1000 * 60 * 10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("hello - 2");
}
}
上面的代码会睡眠10分钟.让代码不执行
用命令排查
启动上面的main方法
可以看到main线程是time waiting状态了.
多线程同步问题
/** * 演示线程的同步 */
public class ThreadSyncTest {
public static void main(String[] args) {
Number number = new Number();
Thread t1 = new Thread(number);
Thread t2 = new Thread(number);
t1.setName("线程1");
t2.setName("线程2");
t1.start();
t2.start();
}
}
class Number implements Runnable {
private int number = 1;
@Override
public void run() {
while (true) {
synchronized (this) {
if (number <= 100) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":" + number);
number++;
} else {
break;
}
}
}
}
}
上面代码说明,创建两个Number线程 ,然后去给number变量自增,其中锁对象是this, 这样就是同步
边栏推荐
- 高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏
- PTA TIANTI game exercise set l2-003 moon cake test point 2, test point 3 Analysis
- Web Authentication API兼容版本信息
- 成为资深IC设计工程师的十个阶段,现在的你在哪个阶段 ?
- 牙齿干细胞的存储问题(未完待续)
- EMMC print cqhci: timeout for tag 10 prompt analysis and solution
- [daily training -- Tencent selected 50] 292 Nim games
- Loss function and positive and negative sample allocation in target detection: retinanet and focal loss
- Add salt and pepper noise or Gaussian noise to the picture
- 【FPGA教程案例14】基于vivado核的FIR滤波器设计与实现
猜你喜欢
SQL query: subtract the previous row from the next row and make corresponding calculations
An example of multi module collaboration based on NCF
《ClickHouse原理解析与应用实践》读书笔记(6)
【SQL实战】一条SQL统计全国各地疫情分布情况
Differences and introduction of cluster, distributed and microservice
EMMC打印cqhci: timeout for tag 10提示分析与解决
Cf:c. column swapping [sort + simulate]
深度聚类:将深度表示学习和聚类联合优化
I didn't know it until I graduated -- the principle of HowNet duplication check and examples of weight reduction
Loss function and positive and negative sample allocation in target detection: retinanet and focal loss
随机推荐
Flask 1.1.4 werkzeug1.0.1 analyse du code source: processus de démarrage
绕过open_basedir
Hcip seventh operation
C nullable type
Go language context explanation
解决pod install报错:ffi is an incompatible architecture
zabbix_get测试数据库失败
软件测试面试技巧
高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏
得物客服一站式工作台卡顿优化之路
微信小程序蓝牙连接硬件设备并进行通讯,小程序蓝牙因距离异常断开自动重连,js实现crc校验位
Red hat install kernel header file
Red Hat安装内核头文件
Mysql-centos7 install MySQL through yum
980. Different path III DFS
JVM命令之 jinfo:实时查看和修改JVM配置参数
如果不知道这4种缓存模式,敢说懂缓存吗?
Personal imitation SSM framework
深度聚类:将深度表示学习和聚类联合优化
Bat instruction processing details