Package org.jivesoftware.util
Class AutoCloseableReentrantLock
java.lang.Object
org.jivesoftware.util.AutoCloseableReentrantLock
A 
ReentrantLock lock that can be unlocked using an AutoCloseable. This allows for easy locking of
 resources, using a specific class as a namespace. Typical usage:
 
     try (final AutoCloseableReentrantLock.AutoCloseableLock ignored = new AutoCloseableReentrantLock(Clazz.class, user.getUsername()).lock()) {
         user.performNonThreadSafeTask();
     }
 
 This essentially has the same effect as:
     synchronised ((Clazz.class.getName() + user.getUsername()).intern()) {
         user.performNonThreadSafeTask();
     }
 
 but has advantages in that the current status of the lock can interrogated, the lock can be acquired interruptibly, etc.
- 
Nested Class Summary
Nested Classes - 
Constructor Summary
ConstructorsConstructorDescriptionAutoCloseableReentrantLock(Class clazz, String resource) Create a class and resource specific lock. - 
Method Summary
Modifier and TypeMethodDescriptionbooleanQueries if this lock is held by the current thread.booleanisLocked()Queries if this lock is held by any thread.lock()Acquires the lock, blocking indefinitely.Acquires the lock, blocking until the lock is acquired or the thread is interrupted.tryLock()Tries to acquire the lock, returning immediately. 
- 
Constructor Details
- 
AutoCloseableReentrantLock
Create a class and resource specific lock. If another thread has not closed another AutoCloseableReentrantLock with the same class and resource then this will block until it is closed.- Parameters:
 clazz- The class for which the lock should be created.resource- The resource for which the lock should be created.
 
 - 
 - 
Method Details
- 
lock
Acquires the lock, blocking indefinitely.- Returns:
 - An AutoCloseableLock
 - Throws:
 IllegalStateException- if this lock has already been released by the last thread to hold it
 - 
tryLock
Tries to acquire the lock, returning immediately.- Returns:
 - An AutoCloseableLock if the lock was required, otherwise empty.
 - Throws:
 IllegalStateException- if this lock has already been released by the last thread to hold it
 - 
lockInterruptibly
public AutoCloseableReentrantLock.AutoCloseableLock lockInterruptibly() throws InterruptedException, IllegalStateExceptionAcquires the lock, blocking until the lock is acquired or the thread is interrupted.- Returns:
 - An AutoCloseableLock
 - Throws:
 InterruptedException- if the thread was interrupted before the lock could be acquiredIllegalStateException- if this lock has already been released by the last thread to hold it
 - 
isHeldByCurrentThread
public boolean isHeldByCurrentThread()Queries if this lock is held by the current thread.- Returns:
 trueif current thread holds this lock andfalseotherwise- See Also:
 
 - 
isLocked
public boolean isLocked()Queries if this lock is held by any thread. This method is designed for use in monitoring of the system state, not for synchronization control.- Returns:
 trueif any thread holds this lock andfalseotherwise- See Also:
 
 
 -