How to Detect and Resolve Java Deadlocks and 100% CPU Issues: A Step‑by‑Step Guide

This article walks Java developers through diagnosing and fixing common problems such as deadlocks and CPU usage spikes, explaining the underlying concepts, showing practical code examples, and demonstrating how to use tools like jps, jstack, JConsole, JVisualVM, and Arthas to pinpoint and prevent performance bottlenecks.

Programmer DD
Programmer DD
Programmer DD
How to Detect and Resolve Java Deadlocks and 100% CPU Issues: A Step‑by‑Step Guide

Article Overview

As a developer or engineer, you may have encountered questions about Java deadlocks, CPU usage hitting 100%, and tools for quickly inspecting thread activity. This guide summarizes solutions to these three problems with hands‑on examples.

Java Deadlock Investigation and Resolution

To troubleshoot a deadlock, consider four questions:

What is a deadlock?

Why does it occur?

How to detect it in code?

How to avoid writing deadlock‑prone code?

What Is a Deadlock?

A deadlock occurs when two or more threads are each waiting for resources held by the other, causing all of them to block indefinitely. Both processes and threads can deadlock if the necessary conditions are met.

Deadlock illustration
Deadlock illustration

Why Do Deadlocks Occur?

Two essential conditions must be satisfied:

At least two threads are involved.

They compete for shared resources.

How to Detect Deadlocks in Code (Key Part)

First, create a sample deadlock program (image omitted for brevity). Then use one of the following approaches:

Approach 1: jps + jstack

1. Run jps -l to list Java processes.

jps output
jps output

2. Run jstack -l <PID> to dump thread stacks.

jstack output
jstack output

Approach 2: JConsole

Open JConsole (a graphical monitoring tool) and navigate to the Threads tab to view thread states and detect deadlocks.

JConsole UI
JConsole UI

Approach 3: Java VisualVM

Launch jvisualvm, switch to the Threads tab, and look for deadlock warnings.

VisualVM thread view
VisualVM thread view
VisualVM deadlock detection
VisualVM deadlock detection

How to Prevent Deadlocks

Follow consistent lock ordering when acquiring multiple resources. For example, if thread A locks resources A → B → C, thread B must acquire them in the same order to avoid deadlock.

Java CPU 100% Troubleshooting Techniques

When a Java process consumes all CPU, follow these steps:

Use top to identify the PID with high CPU usage.

Run jps -l to map the PID to the Java application.

Execute pidstat -p <PID> 1 3 -u -t to monitor per‑thread CPU usage.

top output
top output

Identify the busy thread ID (e.g., 3467), convert it to hexadecimal ( d8d), and locate the corresponding stack trace with jstack -l <PID>.

-p: specify process ID</code><code>-u: display CPU usage statistics</code><code>-t: include thread‑level details
pidstat output
pidstat output
thread details
thread details

Thread states include NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, and TERMINATED.

Recommended Tools for Fast Problem Diagnosis

1. show-busy-java-threads – a script that lists the busiest Java threads.

show-busy-java-threads output
show-busy-java-threads output

2. Alibaba’s arthas – an interactive troubleshooting tool.

Arthas UI
Arthas UI

Use thread -n 10 to display the top 10 busy threads with stack traces.

Conclusion

This hands‑on tutorial provides a complete workflow for detecting and resolving Java deadlocks and high CPU usage, emphasizing the importance of both understanding the underlying concepts and practicing the described steps.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaperformancedeadlockCPUArthasJVisualVMjstack
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.