Scrivio Interview library
ConceptIntermediate7 min read · updated 12 November 2026

What tools and JFR events would you use to profile contention in a JVM, and what does each tell you?

Java Flight Recorder has three events for contention: jdk.JavaMonitorEnter records threads that waited to enter a synchronized block for longer than a threshold, with their stacks and the monitor's class; jdk.ThreadPark records threads parked by java.util.concurrent locks, conditions and queues; and jdk.JavaMonitorWait records time spent in Object.wait(). async-profiler's lock mode samples contended lock acquisitions and draws them as flame graphs, and JDK Mission Control turns a JFR recording into views grouped by lock class and stack.

Why this question is asked

Lock contention is a performance problem that hides from the usual tools. CPU profilers show nothing, because waiting threads do not use CPU, and logs show nothing, because nothing failed. We need tools that record waiting itself. Interviewers ask this question to see whether we know those tools by name and, more importantly, what each one can and cannot tell us. In this answer, we will recap what contention is, go through the three JFR events and how to record and read them, then look at async-profiler's lock mode and at how Mission Control presents the data.

1A Quick Recap: Contention and Its Two Kinds of Lock

Contention happens when a thread wants a lock that another thread holds, so it has to wait. A little contention is normal. It becomes a problem when many threads wait for the same lock often or for long, which turns parallel work into a queue.

Java has two families of locks, and the tooling treats them differently. The first is the monitor that every object has, used by synchronized methods and blocks. The JVM implements these itself. The second is the java.util.concurrent family, such as ReentrantLock, ReadWriteLock and Semaphore, plus everything built on them like blocking queues and futures. These are Java code that, when a thread has to wait, calls LockSupport.park. So for monitors we record monitor enter events, and for java.util.concurrent we record park events.

2The Three JFR Events

jdk.JavaMonitorEnter is recorded when a thread had to wait to enter a synchronized block and the wait lasted longer than the configured threshold, 20 ms in the default settings profile and lower in the profile settings. Each event has the duration, the waiting thread, its stack trace, the class of the monitor object and the thread that held it when the wait began. This is the event for synchronized contention.

jdk.ThreadPark is recorded when a thread parks for longer than the threshold. It carries the duration, the stack, and the parkedClass, the class of the object the thread parked on, for example ReentrantLock$NonfairSync. Because idle pool threads also park on their queues, many park events are normal waiting for work, so we filter by stack or parked class to find real lock waits.

jdk.JavaMonitorWait records calls to Object.wait() with their duration and the monitor class. Long waits can be perfectly normal, a consumer waiting for work, but they matter when request threads wait on a condition that is signalled late.

Contention tools and what each tells you
Tool or eventWhat it recordsBest for
jdk.JavaMonitorEnterwaits to enter synchronized above a thresholdsynchronized hot spots, with holder thread
jdk.ThreadParkparks by j.u.c. locks, conditions, queuesReentrantLock, queues, futures
jdk.JavaMonitorWaittime inside Object.wait()wait and notify code
async-profiler -e locksampled contended acquisitions, all durationsflame graph of where waits come from
JDK Mission Controlvisual analysis of a JFR filegrouping by lock class, thread and stack

3Recording and Reading the Events

We can start a recording on a running JVM with jcmd, no restart needed. For a contention investigation it is worth lowering the thresholds, so that shorter waits are recorded too. A custom settings file, made by copying the default one from the JDK's lib/jfr folder, sets the threshold per event.

Shell
jcmd 48213 JFR.start name=locks settings=/opt/jfr/locks.jfc duration=5m filename=/tmp/locks.jfr
# locks.jfc: jdk.JavaMonitorEnter threshold=5 ms, jdk.ThreadPark threshold=5 ms
jfr print --events jdk.JavaMonitorEnter /tmp/locks.jfr | head -30
# jdk.JavaMonitorEnter {
# duration = 184 ms
# monitorClass = com.example.PriceCache (classLoader = app)
# previousOwner = "http-nio-8080-exec-3"
# eventThread = "http-nio-8080-exec-41"
# stackTrace = [ com.example.PriceCache.get(String) line: 31 ... ]
# }
jfr summary /tmp/locks.jfr # counts per event type
jfr view --help # JDK 21 and later: built-in tabular views, check your version for the names

Notice the fields that make this event so useful. monitorClass tells us which kind of object is the lock, the stack tells us where threads waited, and previousOwner tells us who was holding it, so we can go and look at what that thread was doing. Summing the durations by stack gives the most expensive lock sites, which is what we really want: not the longest single wait, but where the total waiting time goes.

4Async-profiler in Lock Mode

async-profiler is an open-source profiler for HotSpot JVMs. Besides CPU and allocation profiling it has a lock mode that samples contended lock acquisitions, both monitors and java.util.concurrent locks, and weighs each sample by the time spent waiting. Unlike JFR's threshold-based events, it captures short waits too, so many 2 ms waits that add up show clearly.

Text
asprof -e lock -d 60 -f /tmp/lock.html 48213 # 60 s, flame graph of lock waits
asprof -e lock --lock 1ms -d 60 -f /tmp/lock.html 48213 # only waits longer than 1 ms

The result is a flame graph where the width of each frame is the waiting time spent under it, and the top frames name the lock class. A wide tower ending in PriceCache.get says at once where to look. async-profiler can also write JFR-format output, so its results open in the same tools.

5JDK Mission Control for the Big Picture

JDK Mission Control is the free desktop tool for reading JFR recordings. Its lock instances page groups monitor enter events by the class of the lock object, with total and maximum wait times, and its threads view shows each thread's timeline coloured by state, so a column of red blocked bars across many threads at the same moment is easy to spot. Selecting a group shows the stacks behind it. JMC also has an automated analysis page that flags suspicious patterns, such as high contention on one monitor class. For quick checks on a server, the jfr command-line tool is enough; for exploring a problem, JMC is far more comfortable.

Step throughFrom recording to the contended lock
  1. Start JFR with lower lock thresholdsjcmd JFR.start, a few minutes under load
  2. Sum waits by lock classJavaMonitorEnter and ThreadPark
  3. Pick the class with the largest totalnot the longest single wait
  4. Open the stacks and previous ownerswhat does the holder do inside the lock?
  5. Fix and record againcompare totals

5 steps in order. Play them, or step through them one at a time.

6Follow-up questions

Why is a threshold needed for these events at all?

Because contention events can be extremely frequent. A busy service can have millions of very short lock waits per minute, and recording each one would cost more than the waiting itself. The threshold keeps overhead low enough for continuous recording by keeping only the waits long enough to matter. For a targeted investigation we lower it for a few minutes.

Do these events work with virtual threads?

Yes, JFR records events for virtual threads too, and since Java 24 a virtual thread blocked on synchronized can unmount, so its wait looks like a normal monitor wait. For virtual threads there is also the jdk.VirtualThreadPinned event, which shows when a virtual thread blocks while holding on to its carrier. Check your JDK version, because the details changed between 21 and 24.

When would you prefer async-profiler over JFR?

When short waits matter, because async-profiler samples every contended acquisition regardless of duration, and when I want a flame graph quickly. JFR is better for always-on recording in production, since it is built into the JDK, needs no extra agent and also records the surrounding context such as GC, I/O and CPU in the same file.

7Conclusion

In this answer, we separated monitor contention from java.util.concurrent contention and saw the JFR event for each: jdk.JavaMonitorEnter for waits to enter synchronized, with the monitor class and the previous owner, jdk.ThreadPark for parking on locks, conditions and queues, and jdk.JavaMonitorWait for Object.wait(). We recorded them with jcmd and lower thresholds and read them with jfr print, saw how async-profiler's lock mode samples all contended acquisitions into a flame graph, and used JDK Mission Control to group waits by lock class and stack.

What to say in the interviewAbout 90 seconds aloud

Open with

For contention I start with Java Flight Recorder, because it is built into the JDK and cheap enough for production. There are three events I care about. jdk.JavaMonitorEnter records threads that waited to enter a synchronized block for longer than a threshold, 20 milliseconds in the default profile, with the stack, the class of the monitor and the thread that held it, so I see both who waited and who was holding the lock. jdk.ThreadPark covers the java.util.concurrent side: ReentrantLock, conditions, queues and futures all park through LockSupport, and the event shows the parked class and stack.

Then, if they want more

Since idle pool threads park too, I filter by stack. jdk.JavaMonitorWait records time inside Object.wait, which matters for wait and notify code.

I would start a recording with jcmd JFR.start, lowering the lock thresholds to a few milliseconds for a short investigation, then sum the waiting time by lock class and stack rather than look at the single longest wait. async-profiler in lock mode complements this: it samples contended acquisitions of any length and draws a flame graph weighted by waiting time, which catches many short waits that JFR's threshold would skip.

And JDK Mission Control is where I explore a recording visually, with lock instances grouped by class, thread timelines showing blocked periods, and the stacks behind each group.

About this answer

Last updated
12 November 2026
Versions it names
Java 21, Java 24
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What tools and JFR events would you use to profile contention in a JVM, and what does...": https://scrivio-interview-library.netlify.app/java/concurrency-problems/ef6072d30f/