Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. Download free →
CLSkills
Debuggingadvanced

Deadlock Detector

Share

Find potential deadlocks in concurrent code

Works with OpenClaude

You are a concurrent programming debugger. The user wants to find potential deadlocks in concurrent code by analyzing lock acquisition patterns, thread interactions, and circular wait conditions.

What to check first

  • Run jps -l (Java) or ps aux | grep java to identify running processes with threads
  • Check thread dump: jstack <pid> to see current lock holders and waiters
  • Review code for nested locks, shared resources, and lock ordering inconsistencies

Steps

  1. Capture a thread dump using jstack <pid> > threadump.txt and search for waiting to lock patterns that form cycles
  2. Analyze lock acquisition order across all threads—deadlock requires circular dependencies (Thread A holds lock X waiting for Y, Thread B holds Y waiting for X)
  3. Use ThreadMXBean to programmatically detect deadlocked threads: call ThreadMXBean.findDeadlockedThreads() or findMonitorDeadlockedThreads()
  4. Instrument code with lock ordering verification—assign a numeric priority to each lock and enforce that locks are always acquired in ascending order
  5. Enable -XX:+PrintConcurrentLocks JVM flag to print lock information in thread dumps for clearer analysis
  6. Build a lock acquisition graph: map which threads hold which locks and which locks each thread is waiting for
  7. Check for common patterns: nested synchronized blocks on different objects, callback methods acquiring locks, condition variables without proper signaling
  8. Run static analysis tools like Checker Framework or ThreadSanitizer (for C/C++) to detect potential data races and lock issues before runtime

Code

import java.lang.management.ThreadMXBean;
import java.lang.management.ManagementFactory;

public class DeadlockDetector {
    private static final ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    
    // Lock ordering enforcement
    private static class LockTracker {
        private static final ThreadLocal<java.util.TreeSet<Integer>> lockStack = 
            ThreadLocal.withInitial(java.util.TreeSet::new);
        
        public static void acquireLock(int lockId) {
            java.util.TreeSet<Integer> stack = lockStack.get();
            if (!stack.isEmpty() && stack.last() > lockId) {
                throw new IllegalStateException(
                    "Lock ordering violation: attempting to acquire lock " + lockId + 
                    " while holding higher-priority lock " + stack.last());
            }
            stack.add(lockId);
        }
        
        public static void releaseLock(int lockId) {
            lockStack.get().remove(lockId);
        }
    }
    
    // Periodic deadlock detection
    public static void detectDeadlocks() {
        long[] deadlockedThreads = threadBean.findDeadlockedThreads();
        
        if (deadlockedThreads != null && deadlockedThreads.length > 0) {

Note: this example was truncated in the source. See the GitHub repo for the latest full version.

Common Pitfalls

  • Treating this skill as a one-shot solution — most workflows need iteration and verification
  • Skipping the verification steps — you don't know it worked until you measure
  • Applying this skill without understanding the underlying problem — read the related docs first

When NOT to Use This Skill

  • When a simpler manual approach would take less than 10 minutes
  • On critical production systems without testing in staging first
  • When you don't have permission or authorization to make these changes

How to Verify It Worked

  • Run the verification steps documented above
  • Compare the output against your expected baseline
  • Check logs for any warnings or errors — silent failures are the worst kind

Production Considerations

  • Test in staging before deploying to production
  • Have a rollback plan — every change should be reversible
  • Monitor the affected systems for at least 24 hours after the change

Quick Info

CategoryDebugging
Difficultyadvanced
Version1.0.0
AuthorClaude Skills Hub
debuggingdeadlockconcurrency

Install command:

curl -o ~/.claude/skills/deadlock-detector.md https://claude-skills-hub.vercel.app/skills/debugging/deadlock-detector.md

Related Debugging Skills

Other Claude Code skills in the same category — free to download.

Want a Debugging skill personalized to YOUR project?

This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.