当前位置:网站首页>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, 这样就是同步
边栏推荐
- mac版php装xdebug环境(m1版)
- An example of multi module collaboration based on NCF
- What is make makefile cmake qmake and what is the difference?
- 话说SQLyog欺骗了我!
- 线性回归
- Win configuration PM2 boot auto start node project
- Opensergo is about to release v1alpha1, which will enrich the service governance capabilities of the full link heterogeneous architecture
- Differences and introduction of cluster, distributed and microservice
- 驱动开发中platform设备驱动架构详解
- MFC BMP sets the resolution of bitmap, DPI is 600 points, and gdiplus generates labels
猜你喜欢
Modes of optical fiber - single mode and multimode
Pytorch builds neural network to predict temperature
C nullable type
Hcip seventh operation
产业金融3.0:“疏通血管”的金融科技
R language [logic control] [mathematical operation]
C. colonne Swapping [tri + Simulation]
CTFshow--常用姿势
Introduction to distributed transactions
驱动开发中platform设备驱动架构详解
随机推荐
三级菜单数据实现,实现嵌套三级菜单数据
Distributed global ID generation scheme
一个简单的代数问题的求解
Add salt and pepper noise or Gaussian noise to the picture
An example of multi module collaboration based on NCF
Nvisual network visualization
Go 語言的 Context 詳解
随机生成session_id
What is make makefile cmake qmake and what is the difference?
Red Hat安装内核头文件
cf:C. Column Swapping【排序 + 模拟】
Opensergo is about to release v1alpha1, which will enrich the service governance capabilities of the full link heterogeneous architecture
Type de texte de commutation d'entrée et de mot de passe de l'applet natif
Digital IC interview summary (interview experience sharing of large manufacturers)
绕过open_basedir
SQL query: subtract the previous row from the next row and make corresponding calculations
Dynamic memory management
PTA 天梯赛练习题集 L2-004 搜索树判断
Data storage 3
nVisual网络可视化