当前位置:网站首页>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提示分析与解决
- Jstat of JVM command: View JVM statistics
- 测试开发基础,教你做一个完整功能的Web平台之环境准备
- [daily training -- Tencent selected 50] 235 Nearest common ancestor of binary search tree
- A freshman's summary of an ordinary student [I don't know whether we are stupid or crazy, but I know to run forward all the way]
- Input of native applet switches between text and password types
- 往图片添加椒盐噪声或高斯噪声
- JVM命令之 jinfo:实时查看和修改JVM配置参数
- 搞懂fastjson 对泛型的反序列化原理
- Opensergo is about to release v1alpha1, which will enrich the service governance capabilities of the full link heterogeneous architecture
猜你喜欢
R language [logic control] [mathematical operation]
往图片添加椒盐噪声或高斯噪声
Go语学习笔记 - gorm使用 - 原生sql、命名参数、Rows、ToSQL | Web框架Gin(九)
为不同类型设备构建应用的三大更新 | 2022 I/O 重点回顾
The solution of a simple algebraic problem
On the discrimination of "fake death" state of STC single chip microcomputer
每秒10W次分词搜索,产品经理又提了一个需求!!!(收藏)
nVisual网络可视化
Sequential storage of stacks
Jstat pour la commande JVM: voir les statistiques JVM
随机推荐
Chain storage of stack
If you don't know these four caching modes, dare you say you understand caching?
Jstat pour la commande JVM: voir les statistiques JVM
nVisual网络可视化
Three level menu data implementation, nested three-level menu data
SQL Server 2008 各种DateTime的取值范围
PTA 天梯赛练习题集 L2-003 月饼 测试点2,测试点3分析
10W word segmentation searches per second, the product manager raised another demand!!! (Collection)
Dc-7 target
关于STC单片机“假死”状态的判别
Opensergo is about to release v1alpha1, which will enrich the service governance capabilities of the full link heterogeneous architecture
话说SQLyog欺骗了我!
From "running distractor" to data platform, Master Lu started the road of evolution
深度聚类:将深度表示学习和聚类联合优化
mac版php装xdebug环境(m1版)
Flask1.1.4 werkzeug1.0.1 source code analysis: start the process
Add salt and pepper noise or Gaussian noise to the picture
《ClickHouse原理解析与应用实践》读书笔记(6)
Data storage 3
STM32按键状态机2——状态简化与增加长按功能