---
title: Thread Deadlock Detection - JMX Monitoring Agent
description: Does the Nodinite JMX Monitoring Agent detect thread deadlocks? No. It monitors JVM CPU and free heap. Learn what a deadlock is, what the agent shows, and how to find deadlocks with jstack and JConsole.
---

# <i class="fal fa-rotate-exclamation" aria-hidden="true"></i> Thread Deadlock Detection

**The <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> [JMX Monitoring Agent][JMXAgent] doesn't detect thread deadlocks and doesn't count threads.** It monitors **CPU** and free **heap** per JVM ([Configuration][]). This page explains what a **thread deadlock** is, what you can and can't expect to see in <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span>, and how to find a deadlock with standard JVM tools such as **jstack** and **JConsole**.

## Understanding Thread Deadlocks

A **thread deadlock** occurs when two or more **JVM threads** each hold a lock that another thread is waiting to acquire. None of the threads can proceed; they wait indefinitely, and the work they handle stalls. The JVM itself keeps running, and a deadlock doesn't resolve itself: the application usually needs a restart.

<div class="mermaid">
graph LR
  A["fa:fa-lock Thread A<br/>Holds Lock 1<br/>Waiting for Lock 2"]
  B["fa:fa-lock Thread B<br/>Holds Lock 2<br/>Waiting for Lock 1"]
  A -->|"waiting for"| B
  B -->|"waiting for"| A
  style A fill:#c33d34
  style B fill:#c33d34
</div>

*Diagram: Thread deadlock (red, both threads blocked): Thread A waits for Lock 2 held by Thread B, while Thread B waits for Lock 1 held by Thread A.*

Common causes are acquiring several locks in inconsistent order across code paths, nested synchronized blocks, and third-party libraries or connectors whose internal locks conflict with application locks.

## What the JMX Monitoring Agent Shows During a Deadlock

| Signal | Shown by the JMX Monitoring Agent | Notes |
| --- | --- | --- |
| Deadlocked threads | ❌ No | The agent doesn't read thread information |
| Thread count | ❌ No | The agent doesn't count threads |
| CPU usage | ✅ Yes | Blocked threads use little CPU, so a deadlock often doesn't change CPU at all |
| Free heap | ✅ Yes | Heap may drop if work queues up behind the blocked threads, but that isn't guaranteed |
| JVM reachable | ✅ Yes | The Resource shows Unavailable only if the JVM stops answering JMX, which a deadlock usually doesn't cause |

<aside aria-label="Important" class="nodinite-note nodinite-note--important">
<p class="nodinite-note__label" aria-hidden="true"><i class="far fa-fw fa-message-exclamation"></i> Important</p>
<div class="nodinite-note__content"><p>A JVM Resource can stay OK during a deadlock. Don't rely on the JMX Monitoring Agent to catch deadlocks; watch the work the application does instead, for example with <a href="https://docs.nodinite.com/logging-and-monitoring/non-events/overview/">Non-Events Monitoring</a> on expected message volumes or the <a href="https://docs.nodinite.com/logging-and-monitoring/web-services/overview/">Web Services Monitoring Agent</a> on the application's endpoints.</p></div>
</aside>

## Finding a Deadlock with JVM Tools

### Generate a Thread Dump with jstack

A **thread dump** captures the state of all **JVM threads** at a point in time. Run [jstack](https://docs.oracle.com/en/java/javase/21/docs/specs/man/jstack.html)<sup><i class="fal fa-arrow-up-right-from-square" aria-hidden="true"></i></sup> on the JVM host:

```bash
# Find the process ID
jps -l

# Write a thread dump to a file
jstack <PID> > thread-dump.txt
```

<aside aria-label="Tip" class="nodinite-note nodinite-note--tip">
<p class="nodinite-note__label" aria-hidden="true"><i class="far fa-fw fa-lightbulb"></i> Tip</p>
<div class="nodinite-note__content"><p>Capture 2–3 thread dumps about 15 seconds apart. Threads that are blocked in every dump are deadlocked or under heavy lock contention.</p></div>
</aside>

### Read the Thread Dump

When the JVM finds a deadlock, jstack ends the dump with a summary like this:

```text
Found one Java-level deadlock:
=============================
"Thread-A": waiting to lock monitor 0x00000000f8b3c420
  which is held by "Thread-B"
"Thread-B": waiting to lock monitor 0x00000000f8b3c500
  which is held by "Thread-A"
```

Look for `BLOCKED (on object monitor)` (the thread waits for a lock), `waiting to lock <address>`, and `locked <address>` in the stack trace of each **blocked thread**.

### Use JConsole

[JConsole](https://docs.oracle.com/en/java/javase/21/management/using-jconsole.html)<sup><i class="fal fa-arrow-up-right-from-square" aria-hidden="true"></i></sup> connects to the same JMX remote port that the <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> JMX Gateway uses. On the **Threads** tab, click **Detect Deadlock** to list deadlocked threads and their stack traces.

## Resolving Thread Deadlocks

Restart the JVM to release the locks, then capture a thread dump after startup to confirm the deadlock doesn't reappear at once. To prevent recurring deadlocks:

* <i class="fal fa-arrow-down-1-9" aria-hidden="true"></i> **Consistent lock ordering** - Always acquire locks in the same order across all code paths.
* <i class="fal fa-hourglass-half" aria-hidden="true"></i> **Lock timeouts** - Use `ReentrantLock.tryLock(timeout)` instead of `synchronized` blocks so a thread gives up instead of waiting forever.
* <i class="fal fa-compress" aria-hidden="true"></i> **Reduce lock scope** - Keep the code inside synchronized blocks short.
* <i class="fal fa-shield-halved" aria-hidden="true"></i> **Thread-safe data structures** - Use `java.util.concurrent` collections to reduce explicit locking.

## <i class="fal fa-hand-point-right" aria-hidden="true"></i> Next Steps

<i class="fal fa-edit" aria-hidden="true"></i> [Configuration][]  
<i class="fal fa-display" aria-hidden="true"></i> [OS vs JVM Monitoring][]  

## <i class="fal fa-link-horizontal" aria-hidden="true"></i> Related Topics

<i class="fab fa-java" aria-hidden="true"></i> [Supported Java Versions][]  
<i class="fal fa-server" aria-hidden="true"></i> [Multi-JVM Monitoring][]  
<i class="fal fa-memory" aria-hidden="true"></i> [Heap Used vs Committed][]  
<i class="fab fa-java" aria-hidden="true"></i> [JMX Monitoring Agent][JMXAgent]  
<i class="fal fa-circle-question" aria-hidden="true"></i> [JMX Troubleshooting Overview][Troubleshooting]  

[Configuration]:https://docs.nodinite.com/logging-and-monitoring/jmx/getting-started/configuration/
[Heap Used vs Committed]:https://docs.nodinite.com/logging-and-monitoring/jmx/troubleshooting/heap-used-vs-committed/
[JMXAgent]:https://docs.nodinite.com/logging-and-monitoring/jmx/overview/
[Multi-JVM Monitoring]:https://docs.nodinite.com/logging-and-monitoring/jmx/troubleshooting/multi-jvm-monitoring/
[Non-Events Monitoring]:https://docs.nodinite.com/logging-and-monitoring/non-events/overview/
[OS vs JVM Monitoring]:https://docs.nodinite.com/logging-and-monitoring/jmx/troubleshooting/os-vs-jvm-monitoring/
[Supported Java Versions]:https://docs.nodinite.com/logging-and-monitoring/jmx/troubleshooting/supported-java-versions/
[Troubleshooting]:https://docs.nodinite.com/logging-and-monitoring/jmx/troubleshooting/overview/
[Web Services Monitoring Agent]:https://docs.nodinite.com/logging-and-monitoring/web-services/overview/