Package org.jivesoftware.util
Class AutoCloseableReentrantLock
- java.lang.Object
-
- org.jivesoftware.util.AutoCloseableReentrantLock
-
public class AutoCloseableReentrantLock extends Object
AReentrantLock
lock that can be unlocked using anAutoCloseable
. 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 Modifier and Type Class Description static class
AutoCloseableReentrantLock.AutoCloseableLock
-
Constructor Summary
Constructors Constructor Description AutoCloseableReentrantLock(Class clazz, String resource)
Create a class and resource specific lock.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
isHeldByCurrentThread()
Queries if this lock is held by the current thread.boolean
isLocked()
Queries if this lock is held by any thread.AutoCloseableReentrantLock.AutoCloseableLock
lock()
Acquires the lock, blocking indefinitely.AutoCloseableReentrantLock.AutoCloseableLock
lockInterruptibly()
Acquires the lock, blocking until the lock is acquired or the thread is interrupted.Optional<AutoCloseableReentrantLock.AutoCloseableLock>
tryLock()
Tries to acquire the lock, returning immediately.
-
-
-
Constructor Detail
-
AutoCloseableReentrantLock
public AutoCloseableReentrantLock(Class clazz, String resource)
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 Detail
-
lock
public AutoCloseableReentrantLock.AutoCloseableLock lock() throws IllegalStateException
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
public Optional<AutoCloseableReentrantLock.AutoCloseableLock> 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, IllegalStateException
Acquires 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:
true
if current thread holds this lock andfalse
otherwise- See Also:
ReentrantLock.isHeldByCurrentThread()
-
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:
true
if any thread holds this lock andfalse
otherwise- See Also:
ReentrantLock.isLocked()
-
-