3

Is there any tool or way that can get all the information about the locks in java?

for example, if there is a java program, it creates two threads, and both threads require locks for some variable. Is there any tools that can output the information like which thread locks which variable?

jitron
  • 135
  • 1
  • 9
  • possible duplicate of [Finding the cause for deadlock in multi threading?](http://stackoverflow.com/questions/15936725/finding-the-cause-for-deadlock-in-multi-threading) – Raman Shrivastava Jun 22 '15 at 22:35

1 Answers1

1

You can use ThreadInfo#getLockedSynchronizers() (JavaDoc) via ThreadMXBean to get array of LockInfo on currently owned locks on threads. LockInfo will tell you just class name & identity hashcode of a lock, but that's sufficient in tracing lock objects.

Art Licis
  • 3,619
  • 1
  • 29
  • 49
  • Thank you! but what I want is getting the thread information of program A by using another program B. Like program A has two threads, and I want to write another program that could know the threads' information in program A. – jitron Jun 22 '15 at 23:09
  • 1
    That's exactly what you can do with JMX. See other questions on how to do it; e.g., http://stackoverflow.com/q/5552960/432259 – Art Licis Jun 22 '15 at 23:19