Search code examples
javamultithreadingscalajvmjfr

How to check which thread is holding the monitor?


I have a performance issue (in a complex Scala code using parallel collections and futures). I have used JFR to check more details and I can see the issue manifests itself as a thread waiting on a monitor object (the thread seems to be waiting in java.util.concurrent.ForkJoinTask#internalWait by calling a wait method of the ForkJoinTask). I would like to know which thread holds this monitor object (and from what function / call-stack the monitor was entered). JFR shows me some kind of an address for the monitor, but I did not find a way to search / filter by this address.

enter image description here

Is there some view in the JMC, a plugin, or some other way how to check who and when is locking and releasing given monitor?


Solution

  • The problem is, a monitor object being waited on, is not technically held by any thread. Such monitor has no "owner". In general, you can't know beforehand, which thread is in charge of calling notify, as it can be any thread, or no thread at all.

    However, if a monitor has already been notified, there will be a JFR event containing the information about Notifier Thread. You can see it on your screenshot: the monitor was notified by scala-execution-context-global-54 thread.