Blocked threads and JFR
By far the best way to know when threads are blocked is to use tools that can look into the
JVM and know at a low level when the threads are blocked. One such tool is the Java Flight
and look for those that are causing a thread to block (threads that are blocked waiting to ac-
quire a monitor, or are waiting to read or, more rarely, write data to a socket).
These events can be easily viewed on the histogram panel of Java Mission Control, as shown
in Figure 9-3 .
Figure 9-3. Threads blocked by a monitor in JFR
In this sample, the lock associated with the HashMap in the sun.awt.AppContext.get()
method was contended 163 times (over 66 seconds), causing an average 31 ms increase in
the response time of the request being measured. The stack trace points out that the conten-
tion stems from the way the JSP is writing a java.util.Date object. To improve the scalab-
ility of this code, a thread-local date formatter could be used instead of simply calling the
date's toString() method.