Tuning Process
Prerequisites
- The server and OS are running properly.
- An SSH remote login tool has been installed on the local PC.
- The Java program to be tuned is running on the target server.
Procedure
You can download the demo for this practice.
- Perform real-time profiling on the program and view the number of threads in each state on the Overview tab page.
It is found that two threads are blocked. A deadlock may occur.
Figure 1 Overview
- Switch to the thread list on the CPU tab page and search for blocked threads.
According to observation, the two threads stay in the blocked state for a while and the thread dump operation is performed for multiple times.
Figure 2 Thread List
- Check the lock analysis diagram on the CPU > Thread Dump tab page.
It is found that a deadlock occurs in the two blocked threads.
Figure 3 Lock Analysis
- Check the raw data on the CPU > Thread Dump tab page.
Obtain the deadlock information based on the original thread dump data.
Figure 4 Thread Dump
- View the tuning suggestion for the deadlock.Figure 5 Tuning suggestion
- Locate the cause of deadlock based on the tuning suggestion.
Search for related items in the code based on the thread dump information. It is found that the deadlock is caused by incorrect sequence in which the two threads hold locks. Tune the program code from DeadLock.java to UnDeadLock.java.
- View the tuned program.
After the sequence in which the two threads hold locks is adjusted, no deadlock occurs in the program.
Figure 6 Overview
Figure 7 Thread List
Summary
Once a deadlock occurs in Java, it is difficult to locate the related code. Because there are many program interference factors, you must be careful about the logic of the lock and unlock code. If lock code logic is involved, it is recommended that the program be executed by a new thread or in the thread pool. The thread must be allocated a name of a specific service logic so that the problem can be easily located once it occurs.
When tuning other programs, you need to perform tuning operations based on the analysis results collected by the Java Profiler and the corresponding tuning suggestions. For details about the tuning roadmap, see this practice.