Suppose I have an instance of java.util.concurrent.locks.Lock
Is it possible to determine whether the lock is held by the current thread?
Assume the lock object only implements the Lock
interface and is not necessarily reentrant, so calls to lock
or tryLock
may not be a good way to check the lock.
The Lock
interface itself does not provide such functionality, but its common implementor, ReentrantLock
has such method: ReentrantLock.isHeldByCurrentThread()
.
Note however, as documentation says, the main purpose of this method is debugging, assertions and testing. If you need it for normal program logic, then probably there's some better solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With