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

边栏推荐
- EMMC打印cqhci: timeout for tag 10提示分析与解决
- POI excel export, one of my template methods
- Flask 1.1.4 werkzeug1.0.1 analyse du code source: processus de démarrage
- Go语学习笔记 - gorm使用 - gorm处理错误 | Web框架Gin(十)
- PTA ladder game exercise set l2-002 linked list de duplication
- Mac version PHP installed Xdebug environment (M1 version)
- PTA ladder game exercise set l2-004 search tree judgment
- SAP ABAP BDC (batch data communication) -018
- cf:C. Column Swapping【排序 + 模擬】
- Bbox regression loss function in target detection -l2, smooth L1, IOU, giou, Diou, ciou, focal eiou, alpha IOU, Siou
猜你喜欢

ML's shap: Based on the adult census income binary prediction data set (whether the predicted annual income exceeds 50K), use the shap decision diagram combined with the lightgbm model to realize the

Red hat install kernel header file

404 not found service cannot be reached in SAP WebService test

EMMC print cqhci: timeout for tag 10 prompt analysis and solution

每秒10W次分词搜索,产品经理又提了一个需求!!!(收藏)

ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用shap决策图结合LightGBM模型实现异常值检测案例之详细攻略

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

Industrial Finance 3.0: financial technology of "dredging blood vessels"

Ctfshow-- common posture

POI excel export, one of my template methods
随机推荐
Bypass open_ basedir
Classic questions about data storage
Detailed explanation of platform device driver architecture in driver development
Wechat applet Bluetooth connects hardware devices and communicates. Applet Bluetooth automatically reconnects due to abnormal distance. JS realizes CRC check bit
DC-7靶机
SAP Spartacus checkout 流程的扩展(extend)实现介绍
Ten stages of becoming a Senior IC Design Engineer. What stage are you in now?
Harmonyos practice - Introduction to development, analysis of atomized services
Go language context explanation
线性回归
Reptile exercises (III)
@pathvariable 和 @Requestparam的详细区别
Dc-7 target
目标检测中的BBox 回归损失函数-L2,smooth L1,IoU,GIoU,DIoU,CIoU,Focal-EIoU,Alpha-IoU,SIoU
Go语学习笔记 - gorm使用 - gorm处理错误 | Web框架Gin(十)
改变ui组件原有样式
404 not found service cannot be reached in SAP WebService test
zabbix_ Get test database failed
Say sqlyog deceived me!
@Detailed differences between pathvariable and @requestparam