ThreadMXBean Finds Deadlocked Threads

Detect a real monitor deadlock at runtime with findDeadlockedThreads, then report each victim together with the thread holding its lock.

JavaResilience

Code

snippet
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.Arrays;
import java.util.Comparator;

public class DeadlockProbe {

    static final Object alice = new Object();
    static final Object bob = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread workerA = new Thread(() -> {
            synchronized (alice) {
                sleepQuietly(200);
                synchronized (bob) { } // 等 workerB 手里的锁
            }
        }, "workerA");
        Thread workerB = new Thread(() -> {
            synchronized (bob) {
                sleepQuietly(200);
                synchronized (alice) { } // 等 workerA 手里的锁
            }
        }, "workerB");
        workerA.start();
        workerB.start();

        // 等两个线程互相卡死,再运行时探测
        Thread.sleep(500);
        ThreadMXBean mx = ManagementFactory.getThreadMXBean();
        long[] deadlocked = mx.findDeadlockedThreads();
        System.out.println(
                "deadlocked threads: " + (deadlocked == null ? 0 : deadlocked.length));

        if (deadlocked != null) {
            Arrays.stream(mx.getThreadInfo(deadlocked))
                    .sorted(Comparator.comparing(ThreadInfo::getThreadName))
                    .forEach(info -> System.out.println(info.getThreadName()
                            + " waits for lock held by " + info.getLockOwnerName()));
        }
        System.exit(0); // 死锁线程永不结束,演示完主动退出 JVM
    }

    private static void sleepQuietly(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}

Manufacturing the deadlock

Two threads take alice then bob and bob then alice, each sleeping 200ms in between — a textbook monitor deadlock that the JVM detects via its own deadlock machinery.

Runtime detection

After a 500ms pause, ManagementFactory.getThreadMXBean().findDeadlockedThreads() returns the two victim thread ids (printed as deadlocked threads: 2), and getThreadInfo maps each id to a ThreadInfo carrying the holder: the sorted output prints workerA waits for lock held by workerB and the mirror line.

The exit note

Deadlocked threads never finish, so the demo calls System.exit(0) after reporting; without it the JVM would hang with the two threads blocked forever.

Interactive Tools for This Topic

FAQ

Why must the demo call System.exit(0)?
The two deadlocked threads never terminate, so the JVM would keep running forever without an explicit exit after the report.
What does findDeadlockedThreads return?
The ids of threads caught in cycles of monitor and ownable-synchronizer waits; null means no deadlock was found, which the demo handles before printing details.