Understanding Java Deadlocks: Causes, Example Code, and Detection Tools
This article explains what a deadlock is, demonstrates a simple Java deadlock example with code, outlines the four necessary conditions, and introduces four practical tools—jstack, jconsole, jvisualvm, and jmc—for diagnosing and resolving deadlocks in Java applications.
Deadlock (Dead Lock) occurs when two or more execution units (processes, threads, or coroutines) wait for each other to release resources, and none can proceed.
Deadlock Example
Below is the simplest Java deadlock demonstration: two locks and two threads are created. Thread 1 acquires lock A, waits one second, then tries to acquire lock B; Thread 2 does the opposite, leading to a circular wait.
public class DeadLockExample {
public static void main(String[] args) {
Object lockA = new Object(); // create lock A
Object lockB = new Object(); // create lock B
// Thread 1
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
// acquire lock A
synchronized (lockA) {
System.out.println("线程 1:获取到锁 A!");
try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }
System.out.println("线程 1:等待获取 B...");
synchronized (lockB) {
System.out.println("线程 1:获取到锁 B!");
}
}
}
});
t1.start(); // run thread
// Thread 2
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
// acquire lock B
synchronized (lockB) {
System.out.println("线程 2:获取到锁 B!");
try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }
System.out.println("线程 2:等待获取 A...");
synchronized (lockA) {
System.out.println("线程 2:获取到锁 A!");
}
}
}
});
t2.start(); // run thread
}
}The execution result shows both threads waiting for each other, confirming a deadlock.
Causes of Deadlock
To cause a deadlock, the following four conditions must be simultaneously satisfied:
Mutual Exclusion : A resource can be held by only one execution unit at a time.
Hold and Wait : An execution unit holds at least one resource while requesting additional resources that are held by others.
No Preemption : Resources already allocated cannot be forcibly taken away.
Circular Wait : A circular chain of execution units exists, each waiting for a resource held by the next unit in the chain.
Only when all four conditions are present does a deadlock occur.
Deadlock Diagnosis
If a program experiences a deadlock, any of the following four methods can be used for analysis.
Solution 1: jstack
First obtain the process ID (PID) with jps -l, then run jstack -l PID to reveal deadlock information.
PS: Use jstack -help for more options.
Solution 2: jconsole
Launch jconsole from the JDK bin directory, connect to the target JVM, switch to the “Threads” tab, and click “Detect deadlock”.
Solution 3: jvisualvm
Open jvisualvm from the JDK bin directory, select the target JVM, go to the “Threads” tab, and view deadlock details.
Solution 4: jmc
jmc(Java Mission Control) is another tool in the JDK bin directory. Launch it, select the target program, open the JMX console, and enable “Thread” → “Deadlock detection” to view details.
Summary
Deadlock occurs when two or more execution units wait for each other to release resources, causing a standstill. Four tools can be used for detection: jstack, jconsole, jvisualvm, and jmc.
jstack
jconsole
jvisualvm
jmc
Considering ease of use and performance, jconsole or jvisualvm are recommended for deadlock investigation.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Su San Talks Tech
Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
