Sub topic 13
Thread dumps and lock contention
This chapter assumes you can run jcmd, read a stack trace, and know that Java threads share a heap and can hold locks. A slow application whose CPU use is low often has threads waiting on each other. A CPU profile will not show that. A thread dump will.
What a thread dump shows
A thread dump is a snapshot of every thread's name, state, and stack. The states that matter here are:
RUNNABLE: running, or ready to run.BLOCKED: waiting to enter asynchronizedblock that another thread holds.WAITINGorTIMED_WAITING: waiting forwait(),park(), orsleep(), usually because of a queue, a lock fromjava.util.concurrent, or I/O.
Reproduce contention
This program starts four threads that share one lock. Each holds the lock while sleeping, so the others queue behind it.
public class Contended {
private static final Object LOCK = new Object();
public static void main(String[] args) {
for (int i = 0; i < 4; i++) {
Thread t = new Thread(() -> {
while (true) {
synchronized (LOCK) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
return;
}
}
}
}, "worker-" + i);
t.start();
}
}
}Find its process ID with jcmd, then capture a dump:
jcmd <pid> Thread.print > threads.txtRead the dump
Search threads.txt for BLOCKED. A blocked thread looks like this:
"worker-1" #22 prio=5 tid=0x... nid=0x... waiting for monitor entry
java.lang.Thread.State: BLOCKED (on object monitor)
at Contended.lambda$main$0(Contended.java:7)
- waiting to lock <0x000000070fe1a2b8> (a java.lang.Object)The address after waiting to lock identifies the lock. Search for the same address with locked to find the owner:
"worker-0" ...
java.lang.Thread.State: TIMED_WAITING (sleeping)
- locked <0x000000070fe1a2b8> (a java.lang.Object)Here the owner is sleeping while holding the lock. That is the bug: slow work inside a critical section.
Take several dumps
One dump is a single moment. Take three or four, a few seconds apart. A lock address that appears in the same threads every time is real contention. If every thread is blocked on locks held by each other, Thread.print ends with Found one Java-level deadlock, followed by the cycle.
Measure with JFR
Dumps show that contention exists. They don't show how much time it costs. JFR records jdk.JavaMonitorEnter events, which give how long each thread waited and which monitor it waited for. It also records jdk.JavaMonitorWait events. JFR only records waits above a threshold, which is 10 to 20 ms in the bundled settings. Short, frequent waits can be missing from the recording. In JDK Mission Control, open the Lock Instances page to see which monitors cost the most total wait time.
Typical fixes
- Move slow work (I/O, sleeping, remote calls) outside the
synchronizedblock. - Shrink the critical section to the shared state only.
- Split one lock into several, for example per-key locks, or use
ConcurrentHashMap. - Replace a shared counter with
LongAdder.
Re-measure after each change, as the optimize and verify workflow requires.
Exercise
In a thread dump, five threads show BLOCKED with waiting to lock <0x0000000712345678>. How do you find the thread that holds the lock, and what do you check next?
Show answer
Search the dump for locked <0x0000000712345678>. The thread with that line is the owner. Check its stack and state to see what it is doing while holding the lock, such as I/O or sleeping. Then take more dumps to confirm the pattern repeats.