/** * Queries if this lock is held by the current thread. * * <p>Analogous to the {@link Thread#holdsLock} method for built-in monitor locks, this method is * typically used for debugging and testing. For example, a method that should only be called * while a lock is held can assert that this is the case: * * <pre> * class X { * ReentrantLock lock = new ReentrantLock(); * // ... * * public void m() { * assert lock.isHeldByCurrentThread(); * // ... method body * } * } * </pre> * * <p>It can also be used to ensure that a reentrant lock is used in a non-reentrant manner, for * example: * * <pre> * class X { * ReentrantLock lock = new ReentrantLock(); * // ... * * public void m() { * assert !lock.isHeldByCurrentThread(); * lock.lock(); * try { * // ... method body * } finally { * lock.unlock(); * } * } * } * </pre> * * @return <tt>true</tt> if current thread holds this lock and <tt>false</tt> otherwise. */ public boolean isHeldByCurrentThread() { return sync.isHeldByCurrentThread(); }