This chapter explains how to investigate application freezes caused by deadlocks by examining thread dumps, which provide a snapshot of all JVM threads and their states at a specific moment. A deadlock happens when threads wait on one another in a cycle, preventing progress and often freezing part or all of an application. Because a frozen program is difficult to inspect with normal sampling or profiling, thread dumps become the key diagnostic tool for identifying what each thread is doing and how they are interacting.
The chapter demonstrates deadlock detection with a simple example involving two threads that access two shared lists using nested synchronized blocks in opposite lock order. This setup makes it easy for the threads to block each other, showing how even small synchronization mistakes can stop execution. It also notes that deadlocks are not limited to synchronized blocks; other coordination mechanisms such as semaphores, latches, and barriers can create similar problems, so the investigation method remains broadly useful.
To collect thread dumps, the chapter presents two main approaches: using a profiler such as VisualVM or JProfiler, and using command-line tools when remote access or production constraints make profiling impractical. With the command line, the workflow is to find the JVM process ID with jps -l, generate the dump with jstack <PID>, and then save or import the text for analysis. The chapter also explains how to read raw thread dump output by focusing on thread names, IDs, states, stack traces, and lock information, and shows a practical deadlock-finding method: filter blocked threads, trace each waiting lock, and follow the dependency chain until it loops back. Finally, it introduces fastThread as a visualization aid that can summarize the dump, detect deadlocks, and make complex thread relationships easier to understand, while emphasizing that raw thread dumps remain valuable when no tool is available.
If both threads enter the outer synchronized block, but not the inner one, they remain stuck and wait for each other. We say that they went into a deadlock.
When some of the app’s threads get into a deadlock, VisualVM indicates the situation with a message in the Threads tab. Notice that both the _Consumer and _Producer threads are locked on the graphic timeline. To get a thread dump, you simply select the Thread Dump button in the window’s upper-right corner.
A thread dump in plain text that describes an app’s threads. In the thread dump we collected, we can find the two deadlocked threads _Consumer and _Producer.
Follow three simple steps to get a thread dump using the command line. First, find the process ID for which you want the thread dump. Second, use a JDK tool to get the thread dump. Finally, open the thread dump in a profiler tool to read it.
By using the jps tool provided with the JDK, we get the PIDs of the running Java processes. These PIDs are necessary to get thread dumps for a given process.
The jstack command followed by a PID will generate a thread dump for the given process. The thread dump is shown as plain text (also called a raw thread dump). You can collect the text in a file to import it and investigate it later.
Once you save the thread dump into a file, you can open it in various tools to investigate it. For example, to open it in VisualVM, you select File > Load.
To find a deadlock with a thread dump, follow these three easy steps. First, remove all threads that are not blocked. Then, start with one of the blocked threads and find what is blocking it using the lock ID. Continue this process for each thread. If you return to a thread you already investigated, it means you found a deadlock.
When only two threads block each other, it’s called a simple deadlock, but a deadlock can be caused by multiple threads that block each other. More threads means more complexity. Thus, when more than two threads are involved, it's called a complex deadlock.
Cascading locks appear when multiple threads enter a chain where they wait for one another. The last thread in the chain is blocked by an external event, such as reading from a data source or calling an endpoint.
To analyze a thread dump, upload a file containing the thread dump raw data to fastThread.io and wait for the tool to present the details in a simple-to-understand shape.
fastThread provides various details in an easy-to-read format. These details include deadlock detection, dependency graphs, resource consumption, and a flame graph.
After analyzing the thread dump raw data, fastThread identifies and provides details about the deadlock caused by the _Consumer and _Producer threads.
Summary
- When two or more threads get blocked while waiting for each other, they are in a deadlock. When an app gets into a deadlock, it usually freezes and can’t continue its execution.
- You can identify the root cause of a deadlock using thread dumps, which show the status of all threads of an app at the time the thread dump was generated. This information allows you to find which thread is waiting for another.
- A thread dump also shows details such as resource consumption and stack traces for each thread. If these details are sufficient, you can use a thread dump instead of instrumentation for your investigation. Imagine the difference between a thread dump and profiling as the difference between a picture and a movie. With a thread dump, you only have a still image, so you miss the execution dynamics, but you can still get a lot of relevant and helpful details.
- The thread dump provides information about the threads that were executing in the app when the dump was taken. The thread dump shows essential details about the threads in a plain-text format, including resource consumption, thread state in its life cycle, if the thread is waiting for something, and which locks it’s causing or being affected by.
- You can generate a thread dump with either a profiler or using the command line. Using a profiling tool to get the thread dump is the easiest approach, but when you can’t connect a profiler to the running process (e.g., due to network constraints), you can use the command line to get the dump. The thread dump will allow you to investigate the running threads and the relationships between them.
- The plain-text thread dump (also known as a raw thread dump) can be challenging to read. Tools such as fastThread.io help you to visualize the details.
FAQ
What is a thread dump, and why is it useful when investigating deadlocks?
A thread dump is a snapshot of all threads in a JVM process at a specific moment in time. It is especially useful when an application becomes unresponsive because it shows thread states, stack traces, and lock information that can help identify deadlocks and other blocking issues.Why is a profiler often not the best tool for diagnosing a deadlocked application?
When an application is deadlocked, it may be frozen or partially frozen, so live profiling and sampling can be unreliable or impossible. A thread dump is better in this situation because it captures the thread states without needing the app to keep running normally.What are the main ways to obtain a thread dump in Java?
You can obtain a thread dump either through a profiler tool such as VisualVM or JProfiler, or from the command line using JDK tools. The command-line approach is especially useful for remote or server environments.How do you get a thread dump using VisualVM?
Open the application in VisualVM, go to the Threads tab, and use the Thread Dump button in the upper-right corner. VisualVM may even detect deadlocked threads and show a warning in the interface.What are the three command-line steps to collect a thread dump?
First, find the process ID (PID) of the Java application. Second, use a JDK tool such asjstack to generate the raw thread dump. Third, save or import that output into a profiler or analysis tool for easier reading.How do you find the PID of a running Java application?
You can use thejps -l command from the JDK. It lists running Java processes with their PIDs and main class names, making it easy to identify the target process.What command generates a thread dump from the command line?
Thejstack <PID> command generates a thread dump for the Java process with the specified PID. The output is plain text and can be redirected to a file for later investigation.