418928@cepsz.unizar.es wrote:
I'd like to know if there is any way to know which execution thread
holds a lock. That is, I would like to know, before a block
synchronized(o), if any other thread holds the lock to o (for
debugging, for example, because I think that no other thread should
have the lock at that moment). I think it's not possible, not even with
the new ReentrantLocks, but just in case you may have any suggestion to
debug these kind of things...
It just occurred to me that you can get hold of the owner thread for
ReentrantLocks, as it does have the thread handling structure available
at the Java level. Use getOwner(), either by subclassing or reflection
with setAccessible. Just to see a any thread holds the lock, then there
is ReentrantLock.isLocked(). Even for a Lock, to see if another thread
holds the lock, you could do:
final boolean otherHasLock;
if (lock.tryLock()) {
lock.unlock(); // Look! No try-finally.
otherHasLock = false;
} else {
otherHasLock = true;
}
(Note the note on ReentrantLock.isLocked(): "This method is designed for
use in monitoring of the system state, not for synchronization control.")
Tom Hawtin
"lock.unlock()". They probably have a deadlock issue, and are trying
to debug it. I can't be sure though.