当前位置:网站首页>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

边栏推荐
- C note 13
- SAP Spartacus checkout 流程的扩展(extend)实现介绍
- Go语学习笔记 - gorm使用 - 原生sql、命名参数、Rows、ToSQL | Web框架Gin(九)
- 【SQL实战】一条SQL统计全国各地疫情分布情况
- Dc-7 target
- Digital IC interview summary (interview experience sharing of large manufacturers)
- JVM命令之 jinfo:实时查看和修改JVM配置参数
- If you don't know these four caching modes, dare you say you understand caching?
- Flask1.1.4 werkzeug1.0.1 source code analysis: start the process
- Check point: the core element for enterprises to deploy zero trust network (ztna)
猜你喜欢

Digital IC interview summary (interview experience sharing of large manufacturers)

JVM命令之 jstack:打印JVM中线程快照

mac版php装xdebug环境(m1版)

Dc-7 target

如果不知道这4种缓存模式,敢说懂缓存吗?

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

Mac version PHP installed Xdebug environment (M1 version)

cf:C. Column Swapping【排序 + 模擬】

404 not found service cannot be reached in SAP WebService test

nVisual网络可视化
随机推荐
原生小程序 之 input切换 text与password类型
改变ui组件原有样式
目标检测中的BBox 回归损失函数-L2,smooth L1,IoU,GIoU,DIoU,CIoU,Focal-EIoU,Alpha-IoU,SIoU
Red hat install kernel header file
Check Point:企业部署零信任网络(ZTNA)的核心要素
CMD permanently delete specified folders and files
EMMC print cqhci: timeout for tag 10 prompt analysis and solution
From "running distractor" to data platform, Master Lu started the road of evolution
Explication contextuelle du langage Go
jvm命令之 jcmd:多功能命令行
[shell] clean up nohup Out file
make makefile cmake qmake都是什么,有什么区别?
Web authentication API compatible version information
[cloud native] what is the microservice architecture?
【FPGA教程案例14】基于vivado核的FIR滤波器设计与实现
搞懂fastjson 对泛型的反序列化原理
Interview skills of software testing
Opensergo is about to release v1alpha1, which will enrich the service governance capabilities of the full link heterogeneous architecture
What is make makefile cmake qmake and what is the difference?
数字IC面试总结(大厂面试经验分享)