当前位置:网站首页>Deadlock analysis using jstack, jconsole, and jvisualvm
Deadlock analysis using jstack, jconsole, and jvisualvm
2022-07-29 05:08:00 【fen_ fen】
Deadlock concept :
Deadlock refers to the execution of two or more processes , A blocking phenomenon caused by competition for resources or communication with each other , If there is no external force , They will not be able to move forward . At this point, the system is said to be in a deadlock state or the system has produced a deadlock , These processes that are always waiting for each other are called deadlock processes .
When the program gets stuck during execution , Check whether there is deadlock , Available gadgets Jstack and Jconsole To perform deadlock analysis .
1、 Use Jstack Perform deadlock analysis
# First, through jps View the currently running Java process
$jps
# adopt jstack Deadlock analysis , You can view the current deadlock details
jstack 24404
Found one Java-level deadlock:
=============================
"Thread-1":
waiting to lock monitor 0x000000001ca03578 (object 0x000000076c9fc6b0, a java.lang.String),
which is held by "Thread-0"
"Thread-0":
waiting to lock monitor 0x000000001ca020d8 (object 0x000000076c9fc6e0, a java.lang.String),
which is held by "Thread-1"
Java stack information for the threads listed above:
===================================================
"Thread-1":
at runnable.DeadLockTest$2.run(DeadLockTest.java:48)
- waiting to lock <0x000000076c9fc6b0> (a java.lang.String)
- locked <0x000000076c9fc6e0> (a java.lang.String)
at java.lang.Thread.run(Thread.java:748)
"Thread-0":
at runnable.DeadLockTest$1.run(DeadLockTest.java:33)
- waiting to lock <0x000000076c9fc6e0> (a java.lang.String)
- locked <0x000000076c9fc6b0> (a java.lang.String)
at java.lang.Thread.run(Thread.java:748)
Found 1 deadlock.
2. Use Jconsole Perform deadlock analysis
2.1、 First start the deadlock test demo Program
2.2、 stay cmd Run in jconsole Open the graphical interface tool
2.3、 Select the process we need to analyze to connect
2.4、 Select the thread option , Click the bottom detection Deadlock function
2.5、 Then click deadlock , Check the deadlock of each thread
3、 Use jvisualvm Perform deadlock analysis
3.1、 double-click jvisualvm.exe Pop up screen
3.2、 choice demo Program
3、 Click thread , Enter the thread page
4、 choice Thread-1 or Thread-0, Click thread Dump, Enter the page , Pull to the bottom , You can see
Found 1 deadlock
4、Java demo Program
4.1、Demo Example
package runnable;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
import java.util.Date;
/**
* ClassName DeadLockTest
* description DeadLockTest
*
* @author : HMF
* date: 2022/7/27 15:28
**/
public class DeadLockTest {
private static String A="A";
private static String B="B";
public static void main(String[] args){
new DeadLockTest().DeadLock();
}
public void DeadLock(){
Thread t1=new Thread(new Runnable() {
@Override
public void run() {
synchronized (A){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (B){
System.out.println("A");
}
}
}
});
Thread t2=new Thread(new Runnable() {
@Override
public void run() {
synchronized (B){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (A){
System.out.println("B");
}
}
}
});
t1.start();
t2.start();
}
}
4.2、 Execution results
Execution effect ( The console has no output , But the code keeps running ):
4.3、 Deadlock analysis
As you can see from the code above : It's because of resource competition
- Threads t1 Execute first , lock A, wait until t1 To lock B when , Find out B Has been t2 lock , Can only wait for .
- Threads t2 perform , lock B, wait until t2 To lock A when , Find out A Has been t1 lock , Can only wait for .
The last two threads wait for each other , Resources compete with each other , This is a deadlock .
4.4、 How to avoid deadlock ?
We found that Several common methods :
1、 Avoid one thread acquiring multiple locks at the same time . ·
2、 Avoid a thread taking up multiple resources in the lock at the same time , Try to make sure that each lock occupies only one resource .
3、 Try using a time lock , Use lock.tryLock(timeout) Instead of using the internal locking mechanism . ·
For database locks , Lock and unlock must be in a database connection , Otherwise, the unlocking will fail .
remarks :
analysis java Tools for thread deadlock , Recommended jstack command , After all, other 2 Tools in Linux None of them .
jstack By finding the class entry , Then find out which thread the current thread is waiting for , Then locate the number of deadlock rows .
边栏推荐
- Academic | [latex] super detailed texlive2022+tex studio download installation configuration
- 五个关联分析,领略数据分析师一大重要必会处理技能
- Double type nullpointexception in Flink flow calculation
- 优炫数据库启动失败,报网络错误
- [2022 freshmen learning] key points of the third week
- Solve the warning prompt of MySQL mapping file
- pytorch学习笔记
- 传奇开区网站如何添加流量统计代码
- 2021-11-02
- Cache penetration, cache breakdown, cache avalanche and Solutions
猜你喜欢
Five correlation analysis, one of the most important skills of data analysts
Unity metaverse (III), protobuf & socket realize multi person online
[untitled]
WPS插入超链接无法打开,提示“无法打开指定文件”怎么办!
Use jupyter (2) to establish shortcuts to open jupyter and common shortcut keys of jupyter
Sguard64.exe ace guard client exe: frequent disk reading and writing, game jamming, and Solutions
Pytorch learning notes
How to make the characters in the photos laugh? HMS core video editing service one click smile function makes people smile more naturally
[file download] easyexcel quick start
What if excel is stuck and not saved? The solution of Excel not saved but stuck
随机推荐
如何让照片中的人物笑起来?HMS Core视频编辑服务一键微笑功能,让人物笑容更自然
AUTOSAR from introduction to proficiency 100 lectures (78) -autosar-dem module
Conv1d of torch
JS daily question (11)
tmux随笔
Network Security Learning - Intranet Security 1
WPS如何进行快速截屏?WPS快速截屏的方法
The song of the virtual idol was originally generated in this way!
Simple user-defined authentication interface rules
Using jupyter (I), install jupyter under windows, open the browser, and modify the default opening address
深度学习刷SOTA的一堆trick
Northeast University Data Science Foundation (matlab) - Notes
Download addresses of various versions of MySQL and multi version coexistence installation
Vivo market API event reporting and docking
Introduction of JDBC preparestatement+ database connection pool
JS daily question (12)
Big silent event Google browser has no title
Academic | [latex] super detailed texlive2022+tex studio download installation configuration
关于servlet中实现网站的页面跳转
MySQL regularly calls preset functions to complete data update