当前位置:网站首页>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 .
边栏推荐
- On prepayment of house purchase
- Huawei ilearning AI mathematics foundation course notes
- 五个关联分析,领略数据分析师一大重要必会处理技能
- Deadlock to be resolved
- Diagram of odoo development tutorial
- 1 句代码,搞定 ASP.NET Core 绑定多个源到同一个类
- The representation of time series analysis: is the era of learning coming?
- 时间序列分析的表示学习时代来了?
- PHP determines whether the user has logged in. If logged in, the home page will be displayed. If not, enter the login page or registration page
- Create a mindscore environment in modelars, install mindvision, and conduct in-depth learning and training (Huawei)
猜你喜欢

Network Security Learning - Intranet Security 1

Download addresses of various versions of MySQL and multi version coexistence installation

WPS insert hyperlink cannot be opened. What should I do if I prompt "unable to open the specified file"!

MySQL定时调用预置函数完成数据更新

Excel怎么筛选出自己想要的内容?excel表格筛选内容教程

A little knowledge about management

How to make the characters in the photos laugh? HMS core video editing service one click smile function makes people smile more naturally

时间序列分析的表示学习时代来了?

How to avoid damage of oscilloscope current probe

Opencv learning 1 (environment configuration)
随机推荐
JS daily question (12)
Sguard64.exe ace guard client exe: frequent disk reading and writing, game jamming, and Solutions
The song of the virtual idol was originally generated in this way!
Solution to the fourth game of 2022 Hangzhou Electric Multi school league
Wechat picture identification
Solve the warning prompt of MySQL mapping file
Pytorch learning notes
带你搞懂 Kubernetes 集群中几种常见的流量暴露方案
Understand activity workflow
tmux随笔
Conv2d of torch
Numpy basic learning
网安学习-内网安全1
Use more flexible and convenient Rogowski coil
Force deduction ----- sort odd and even subscripts respectively
Unity metaverse (III), protobuf & socket realize multi person online
那个准时上下班,从不愿意加班加点的人,在我前面升职了...
Youxuan database failed to start and reported network error
[wechat applet -- solve the alignment problem of the last line of display:flex. (discontinuous arrangement will be divided into two sides)]
优炫数据库启动失败,报网络错误