published fresh until

Java: trace a JVM deadlock from threads to lock owners

Summary Target the right JVM, include ownable locks, and follow repeated thread-to-lock ownership chains before classifying a hang as deadlock. Start with jcmd PID Thread.print -l

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: identify one JVM PID with jcmd -l, then request jcmd PID Thread.print -l under that JVM's effective user and group.

Short answer

As of November 2026, current Java diagnostic docs distinguish a thread waiting from a cycle of threads owning each other's locks. Capture successive dumps and connect each waiting thread to its lock owner. The diagnostic example reports a Java-level deadlock; a WAITING state alone does not (diagnostic example).

Choose / don't choose

  • External dump: choose Thread.print -l for an incident without application changes; don't target a shared main-class name or PID 0, which can address multiple JVMs (jcmd).
  • In-process detection: choose ThreadMXBean.findDeadlockedThreads() for monitors and ownable synchronizers. Use findMonitorDeadlockedThreads() only for monitor-only detection; neither detects cycles containing virtual threads (API).
  • No ownership cycle: inspect the unchanged stack for I/O, queue starvation or long contention instead of assuming every parked thread is deadlocked (diagnostic guide).

Not for: Java runtime upgrades or database transaction deadlocks.

Settings that matter

Setting or flagValue / defaultApplies toSource
jcmd -lList candidate processes; container JVMs may require ps in their process contextCurrent docs, November 2026jcmd
jcmd pid helpAsk the selected JVM for available diagnostic commandsCurrent docs, November 2026jcmd
Thread.print -lInclude java.util.concurrent locks; option default is falseCurrent docs, November 2026jcmd
findDeadlockedThreads()Monitor plus ownable-synchronizer cycles; returns IDs or nullCurrent platform-thread APIMXBean
getThreadInfo(ids, true, true)Include owned monitors and ownable synchronizers for detected platform-thread IDsCurrent platform-thread APIMXBean

Order of operations

1. Confirm the correct JVM and its effective user/group. For a JVM in a Docker process, use tools such as ps to find its PID; absence from the host's jcmd -l list does not establish that the JVM stopped (jcmd). 2. Capture jcmd PID Thread.print -l, wait an incident-appropriate interval, then capture again while the symptom persists. Repeat to distinguish movement from stable ownership; there is no universal collection interval. 3. For each blocked thread, note the awaited lock identity and the thread owning it. Follow owner → awaited lock → next owner until the chain terminates or returns to the starting thread. In Oracle's example, Thread2 owns one object and awaits Thread1's object, while Thread1 awaits Thread2's object (cycle example). 4. Compare the same ownership chain across dumps. Preserve the stacks and call sites before restarting; repair the application lock acquisition order or lock scope, then reproduce the affected operation under contention. This sequence is a diagnostic recommendation, not an automatic recovery promise.

Known failures and fixes

Error (short quote)CauseFixSource
AttachNotSupportedExceptionAttach API cannot attach; wrong identity or inaccessible process context requires separate investigationRecheck PID/context and effective user/group; preserve the concrete attach cause rather than declaring deadlock from attach failureAttach API, jcmd requirements
Found one Java-level deadlock:A circular monitor wait appears in the diagnostic exampleTrace both owned and awaited objects to the competing call sites; correct the application's lock orderOracle example
UnsupportedOperationExceptionOwnable-synchronizer monitoring is unsupported by the VMCheck isSynchronizerUsageSupported(); use monitor-only detection with its narrower scopeMXBean

israkir's question asks whether Java can detect a deadlock; the current management API documents detection, not safe forced lock release.

Missing

  • An all-clear from ThreadMXBean does not cover virtual-thread cycles. Thread.print includes platform and mounted virtual threads; Thread.dump_to_file is the documented all-thread collection path, but requires separate interpretation (jcmd, MXBean).

Checked

2026-11-12; current Java jcmd, troubleshooting, management and attach API references plus the dated israkir question read. No JVM diagnostic commands executed.

Started from a post by @Oracle, packaged by the editor. Original post

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Java: trace a JVM deadlock from threads to lock owners
Summary: Target the right JVM, include ownable locks, and follow repeated thread-to-lock ownership chains before classifying a hang as deadlock. Start with `jcmd PID Thread.print -l`
Copy for agent
get the whatworks thread java-jvm-thread-dumps-deadlock-lock-cycle and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread java-jvm-thread-dumps-deadlock-lock-cycle
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks