当前位置:网站首页>Jstack of JVM command: print thread snapshots in JVM
Jstack of JVM command: print thread snapshots in JVM
2022-07-07 06:08:00 【Zhang Junjie 1994】
Study Silicon Valley Song Hongkang JVM From entry to mastery Learning notes of
summary
jstack(JVM Stack Trace) It is used to generate a thread snapshot of the current time of the specified process of the virtual machine ( Virtual machine stack trace ), Thread snapshot is the collection of method stacks executed by each thread of the specified process in the current virtual machine .
The function of generating snapshots of threads : There may be a long pause during the execution of multithreads , When threads compete for resources, some threads need to wait for synchronization monitor or deadlock , Or dead circulation , And so on, the above conditions will lead to abnormal threads , There will be a long pause , You want to know which piece of code caused the thread to pause , This is the time jstack Instructions


grammar
Basic grammar
option Parameters :-F
When a normal output request is not responded to , Force output thread stack
option Parameters :-l
Besides the stack , Show additional information about locks
option Parameters :-m
If you call a local method , Can be displayed C/C++ The stack
option Parameters :-h
Help operate
Basic grammar description :

Examples are as follows :
1.
2. Add -l Parameters :
summary :
If the program has a wait problem , You can use this instruction to check the problem , The result will also remind you of the problem
demonstration
Deadlock troubleshooting
/** * Demonstrates thread deadlock problems */
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();// Track all threads in the current process
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();
}
}
explain , The first thread gets s1 lock , Get more s2 lock , The second thread gets s2 lock , Get more s1 lock , In this way, it is easy to deadlock
Check with command
Start the above code


This prints the thread related information ,
The two threads identified by the arrow are blocked , It's because of the deadlock ,
Then it shows deadlock 了 .
Thread sleep troubleshooting
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");
}
}
The above code will sleep 10 minute . Let the code not execute
Check with command
Activate the above main Method

You can see main Thread is time waiting Status quo .
Multithreading synchronization problem
/** * Demonstrate thread synchronization */
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(" Threads 1");
t2.setName(" Threads 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;
}
}
}
}
}
The above code shows , Create two Number Threads , Then go and give it to me number Variable self increasing , The lock object is this, This is synchronization

边栏推荐
- Nvisual network visualization
- JVM命令之 jinfo:实时查看和修改JVM配置参数
- mac版php装xdebug环境(m1版)
- Check Point:企业部署零信任网络(ZTNA)的核心要素
- 深度聚类:将深度表示学习和聚类联合优化
- make makefile cmake qmake都是什么,有什么区别?
- [cloud native] what is the microservice architecture?
- 980. Different path III DFS
- Red hat install kernel header file
- PTA 天梯赛练习题集 L2-004 搜索树判断
猜你喜欢

Nvisual network visualization

OpenSergo 即将发布 v1alpha1,丰富全链路异构架构的服务治理能力

Reptile exercises (III)

【FPGA教程案例13】基于vivado核的CIC滤波器设计与实现

一名普通学生的大一总结【不知我等是愚是狂,唯知一路向前奔驰】

How to improve website weight
![[SQL practice] a SQL statistics of epidemic distribution across the country](/img/ba/639a23d87094d24572a69575b565b9.png)
[SQL practice] a SQL statistics of epidemic distribution across the country

Check Point:企业部署零信任网络(ZTNA)的核心要素

JVM命令之- jmap:导出内存映像文件&内存使用情况

10W word segmentation searches per second, the product manager raised another demand!!! (Collection)
随机推荐
JVM命令之 jstat:查看JVM統計信息
数据中心为什么需要一套基础设施可视化管理系统
Apple CMS V10 template /mxone Pro adaptive film and television website template
980. Different path III DFS
苹果cms V10模板/MXone Pro自适应影视电影网站模板
老板总问我进展,是不信任我吗?(你觉得呢)
[云原生]微服务架构是什么?
[shell] clean up nohup Out file
Jstat pour la commande JVM: voir les statistiques JVM
那些自损八百的甲方要求
Cf:c. column swapping [sort + simulate]
Nvisual network visualization
Loss function and positive and negative sample allocation in target detection: retinanet and focal loss
10W word segmentation searches per second, the product manager raised another demand!!! (Collection)
线性回归
云加速,帮助您有效解决攻击问题!
C note 13
Financial risk control practice - decision tree rule mining template
外设驱动库开发笔记43:GPIO模拟SPI驱动
Go语学习笔记 - gorm使用 - gorm处理错误 | Web框架Gin(十)