Class ReentrantLock.Lock
- java.lang.Object
-
- de.aristaflow.adept2.util.locking.ReentrantLock.Lock
-
- Direct Known Subclasses:
ReentrantLock.ReadLock,ReentrantLock.WriteLock
- Enclosing class:
- ReentrantLock<T>
public class ReentrantLock.Lock extends Object
Represents a reentrant exclusive lock which allows only one object to hold the lock but it may hold it multiple times.
-
-
Field Summary
Fields Modifier and Type Field Description protected booleaninvalidatedWhether this lock has been invalidated and no thread should wait any more.protected LockCountManager<? super T>lockCountManagerThe manager for counting the lock acquisitions of an object and encapsulating whether objects are equal.protected AtomicIntegerwaitersForLockCounts how many threads are waiting to get this lock.
-
Constructor Summary
Constructors Modifier Constructor Description protectedLock(LockCountManager<? super T> lockCountManager)Creates a new exclusive lock using the designatedLockCountManagerfor comparing objects.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description StringcurrentLockingObject()Retrieves a string representation of the object that is currently holding the lock.booleanhasLock(T object)Determines if the designated object holds this lock.voidinvalidate()Invalidates the lock by letting all waiting threads and all future threads throw anInterruptedException.booleanisLocked()Returns whether this lock is currently locked by at least one object.voidlock(T object)Provides the designated object with an exclusive lock.voidlock(T object, long timeout)Provides the designated object with an exclusive lock.protected booleannotificationRequired()Gets whether a notification of waiting threads is required.intobjectsWaitingToLock()Returns the number of objects waiting to acquire this lock.booleanunlock(T object)Releases this lock for the designated object if it holds the lock.
-
-
-
Field Detail
-
lockCountManager
protected final LockCountManager<? super T> lockCountManager
The manager for counting the lock acquisitions of an object and encapsulating whether objects are equal.
-
waitersForLock
protected final AtomicInteger waitersForLock
Counts how many threads are waiting to get this lock.
-
invalidated
protected boolean invalidated
Whether this lock has been invalidated and no thread should wait any more.
-
-
Constructor Detail
-
Lock
protected Lock(LockCountManager<? super T> lockCountManager)
Creates a new exclusive lock using the designatedLockCountManagerfor comparing objects.- Parameters:
lockCountManager- The manager counting locks for objects.
-
-
Method Detail
-
lock
public void lock(T object) throws InterruptedException
Provides the designated object with an exclusive lock. The current thread will be blocked until the lock can be acquired.Calls
Lock.lock(object, 0).- Parameters:
object- The object that requests an exclusive lock.- Throws:
InterruptedException- If the thread is interrupted while waiting for the lock, anInterruptedExceptionwill be thrown.
-
lock
public void lock(T object, long timeout) throws InterruptedException, TimeoutException
Provides the designated object with an exclusive lock. The current thread will be blocked until the lock can be acquired or the timeout elapsed.Since the lock is reentrant, an object may acquire a lock multiple times. But it also has to release the lock per acquisition afterwards.
- Parameters:
object- The object that requests an exclusive lock.timeout- The time to wait for the lock. A timeout <= 0 means to wait forever (or until the lock is granted).- Throws:
InterruptedException- If the thread is interrupted while waiting for the lock, anInterruptedExceptionwill be thrown.TimeoutException- If the designated timeout elapsed before the lock can be acquired, aTimeoutExceptionwill be thrown.
-
hasLock
public boolean hasLock(T object)
Determines if the designated object holds this lock.- Parameters:
object- The object that should be checked to hold this lock.- Returns:
- Whether the designated object holds this lock.
-
isLocked
public boolean isLocked()
Returns whether this lock is currently locked by at least one object.- Returns:
- Whether this lock is currently locked by at least one object.
-
objectsWaitingToLock
public int objectsWaitingToLock()
Returns the number of objects waiting to acquire this lock.- Returns:
- The number of objects waiting to acquire this lock
-
invalidate
public void invalidate()
Invalidates the lock by letting all waiting threads and all future threads throw anInterruptedException.
-
unlock
public boolean unlock(T object)
Releases this lock for the designated object if it holds the lock. If there are other objects waiting for this lock, they will be notified.- Parameters:
object- The object for which the lock should be released.- Returns:
- Whether a notification is sent. This will be done only if the
lock is available now and there are already other objects waiting
for this lock. Otherwise (the lock remains locked by another
object or it is available but there are no waiters for it)
falsewill be returned. - Throws:
IllegalArgumentException- If the designated object does not hold this lock, anIllegalArgumentExceptionwill be thrown.
-
notificationRequired
protected boolean notificationRequired()
Gets whether a notification of waiting threads is required. This depends on whether there are no more locks and whether there are threads waiting for the lock.- Returns:
- Whether a notification of waiting threads is required (no more locks and there are waiters for the lock).
-
currentLockingObject
public String currentLockingObject()
Retrieves a string representation of the object that is currently holding the lock. Due to security reasons the object may not be returned since this would allow to unlock.- Returns:
- A string representation of the current (or one in case of several) locking objects; the empty string will be returned, if the lock is not locked.
-
-