Is it possible at runtime to programmatically check the name of the Thread that is holding the lock of a given object?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can only tell whether the current thread holds a normal lock (
Thread.holdsLock(Object)). You can’t get a reference to the thread that has the lock without native code.However, if you’re doing anything complicated with threading, you probably want to familiarize yourself with the java.util.concurrent packages. The
ReentrantLockdoes 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.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.