1

I need to capture the list of monitors locked by the current thread and dump it into a file. The only functionality that does something similar is kill -3 command to Java VM. Is there a way to do the same thing programmatically?

1 Answers1

3

A variety of useful information about a running JVM and things in it are available from java.lang.management beans. Call ThreadMXBean.getThreadInfo on the bean obtained within the same JVM from ManagementFactory then .getLockedMonitors() and format and write as you wish. From outside the JVM you can access these beans with JMX-RMI (with the same userid on the same machine, or various authentication options on a remote machine) but the current thread might change between one access and the next; you're probably better off getting all threads with .dumpAllThreads() and then finding the one you want in the array.

dave_thompson_085
  • 34,712
  • 6
  • 50
  • 70