当前位置:网站首页>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, 这样就是同步
边栏推荐
- AI face editor makes Lena smile
- 产业金融3.0:“疏通血管”的金融科技
- Type de texte de commutation d'entrée et de mot de passe de l'applet natif
- 搞懂fastjson 对泛型的反序列化原理
- 深度聚类:将深度表示学习和聚类联合优化
- TCC of distributed transaction solutions
- 980. 不同路径 III DFS
- [InstallShield] Introduction
- [shell] clean up nohup Out file
- Classic questions about data storage
猜你喜欢
Web architecture design process
PTA 天梯赛练习题集 L2-004 搜索树判断
EMMC print cqhci: timeout for tag 10 prompt analysis and solution
老板总问我进展,是不信任我吗?(你觉得呢)
R语言【逻辑控制】【数学运算】
Question 102: sequence traversal of binary tree
SAP Spartacus checkout 流程的扩展(extend)实现介绍
Reptile exercises (III)
PTA ladder game exercise set l2-004 search tree judgment
Get the way to optimize the one-stop worktable of customer service
随机推荐
Reading notes of Clickhouse principle analysis and Application Practice (6)
【已解决】记一次EasyExcel的报错【读取xls文件时全表读不报错,指定sheet名读取报错】
苹果cms V10模板/MXone Pro自适应影视电影网站模板
往图片添加椒盐噪声或高斯噪声
Cf:c. column swapping [sort + simulate]
Digital IC interview summary (interview experience sharing of large manufacturers)
【日常训练--腾讯精选50】235. 二叉搜索树的最近公共祖先
Win configuration PM2 boot auto start node project
云加速,帮助您有效解决攻击问题!
Go语学习笔记 - gorm使用 - 原生sql、命名参数、Rows、ToSQL | Web框架Gin(九)
Three level menu data implementation, nested three-level menu data
Mysql-centos7 install MySQL through yum
Go 语言的 Context 详解
力扣102题:二叉树的层序遍历
MySQL-CentOS7通过YUM安装MySQL
Distributed global ID generation scheme
【Shell】清理nohup.out文件
What EDA companies are there in China?
【FPGA教程案例14】基于vivado核的FIR滤波器设计与实现
TCC of distributed transaction solutions