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.
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.
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.
2. Run jstack -l <PID> to dump thread stacks.
Approach 2: JConsole
Open JConsole (a graphical monitoring tool) and navigate to the Threads tab to view thread states and detect deadlocks.
Approach 3: Java VisualVM
Launch jvisualvm, switch to the Threads tab, and look for deadlock warnings.
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.
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 detailsThread 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.
2. Alibaba’s arthas – an interactive troubleshooting tool.
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
