<p>Is it possible at runtime to programmatically check the name of the Thread that is holding the lock of a given object?</p>

[{'answer_id': 50573, 'author': 'erickson', 'author_id': 3474, 'author_profile': 'https://Stackoverflow.com/users/3474', 'pm_score': 6, 'selected': True, 'text': '<p>You can only tell whether the current thread holds a normal lock (<a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#holdsLock%28java.lang.Object%29" rel="noreferrer"><code>Thread.holdsLock(Object)</code></a>). You can\'t get a reference to the thread that has the lock without native code.</p>\n\n<p>However, if you\'re doing anything complicated with threading, you probably want to familiarize yourself with the java.util.concurrent packages. The <a href="http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/ReentrantLock.html" rel="noreferrer"><code>ReentrantLock</code></a> does allow you to get its owner (but its a protected method, so you\'d have to extend this). Depending on your application, it may well be that by using the concurrency packages, you\'ll find that you don\'t need to get the lock\'s owner after all.</p>\n\n<p>There are non-programmatic methods to find the lock owners, such as signaling the JVM to issue a thread dump to stderr, that are useful to determine the cause of deadlocks.</p>\n'}, {'answer_id': 51178, 'author': 'Todd', 'author_id': 3803, 'author_profile': 'https://Stackoverflow.com/users/3803', 'pm_score': 2, 'selected': False, 'text': '<p>Run <a href="http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html" rel="nofollow noreferrer">jconsole</a>.  It is included in the Java SDK and is run from the command line.  I\'m not sure what OS you are using, but on windows you can just pass it the PID of the java process.  It should help you find the thread that is causing the problem.  Or, you can use a commercial profiler like YourKit or any number of other profilers.</p>\n'}, {'answer_id': 51601, 'author': 'Tom Hawtin - tackline', 'author_id': 4725, 'author_profile': 'https://Stackoverflow.com/users/4725', 'pm_score': 3, 'selected': False, 'text': "<p>You can, from 1.6, use JMX to do all sorts of interesting things including finding held locks. You can't get the actual object, but you do get the class and identity hash value (which is not unique).</p>\n<p>(This post originally had a link to an example in my now defunct weblog.)</p>\n"}, {'answer_id': 51620, 'author': 'Chris Kimpton', 'author_id': 48310, 'author_profile': 'https://Stackoverflow.com/users/48310', 'pm_score': 2, 'selected': False, 'text': '<p>In 1.5, you can find all the threads and get each one\'s state, eg like this:</p>\n\n<pre><code>    Map&lt;Thread,StackTraceElement[]&gt; map = Thread.getAllStackTraces();\n    for (Map.Entry&lt;Thread, StackTraceElement[]&gt; threadEntry : map.entrySet()) {\n        log.info("Thread:"+threadEntry.getKey().getName()+":"+threadEntry.getKey().getState());\n        for (StackTraceElement element : threadEntry.getValue()) {\n            log.info("--&gt; "+element);\n        }\n    }\n</code></pre>\n\n<p>Thread.getState gives you info about whether the thread is BLOCKED, WAITING etc, see <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.State.html" rel="nofollow noreferrer">jdk api ThreadState</a></p>\n'}, {'answer_id': 1377751, 'author': 'carrino', 'author_id': 128006, 'author_profile': 'https://Stackoverflow.com/users/128006', 'pm_score': 4, 'selected': False, 'text': '<p>You can get at the locks held by threads with reflection. This only works with java 1.6.</p>\n\n<pre><code>ThreadMXBean bean = ManagementFactory.getThreadMXBean();\nThreadInfo[] ti = bean.getThreadInfo(bean.getAllThreadIds(), true, true);\n</code></pre>\n\n<p>On each of these ThreadInfo objects there are LockInfo objects which you can use the identityHashCode on them to compare to the lock in question.</p>\n'}, {'answer_id': 8379402, 'author': 'dac2009', 'author_id': 827272, 'author_profile': 'https://Stackoverflow.com/users/827272', 'pm_score': 0, 'selected': False, 'text': '<p>You can use a variable to hold the current thread when you take the lock, then print it if someone else is trying to use it.</p>\n\n<pre><code>Thread holderOfLock = null;\nObject theLock = new Object();\n\npublic void doStuff()\n{\n    if(holderOfLock != null)\n    {\n       //get and print name of holderOfLock-thread or get stacktrace etc.\n    }\n\n    synchronized (theLock)\n    {\n        holderOfLock = Thread.currentThread();\n        //do stuff...\n        holderOfLock = null;\n    }\n}\n</code></pre>\n'}, {'answer_id': 15265132, 'author': 'NullPointer', 'author_id': 1047104, 'author_profile': 'https://Stackoverflow.com/users/1047104', 'pm_score': 1, 'selected': False, 'text': '<p>You can check the lock on the particular object by calling <code>wait()</code> or <code>notify()</code> method on that object. If the object does not hold the lock, then it will throw <code>llegalMonitorStateException</code> .</p>\n\n<p>2- By calling <code>holdsLock(Object o)</code> method. This will return the boolean value.</p>\n'}, {'answer_id': 31359345, 'author': 'ceph3us', 'author_id': 4669617, 'author_profile': 'https://Stackoverflow.com/users/4669617', 'pm_score': 2, 'selected': False, 'text': '<p>if it is <strong>re -entrant lock</strong> u can can check if it is held by current thread </p>\n\n<pre><code>final ReentrantLock lock = new ReentrantLock();\nlock.isHeldByCurrentThread();\n</code></pre>\n'}, {'answer_id': 49549659, 'author': 'petertc', 'author_id': 1271412, 'author_profile': 'https://Stackoverflow.com/users/1271412', 'pm_score': 0, 'selected': False, 'text': '<p>Ugly but works.</p>\n\n<pre><code>String findLockOwner(ReentrantLock lock) {\n    String patternStr = "\\\\[Locked by thread (\\\\S+)\\\\]";\n    Pattern pattern = Pattern.compile(patternStr);\n    Matcher matcher = pattern.matcher(lock.toString());\n    boolean matchFound = matcher.find();\n    if (matchFound &amp;&amp; matcher.groupCount() &gt;= 1) {\n      return matcher.group(1);\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/50561', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4220/']