Class ReentrantLock.Lock
java.lang.Object
de.aristaflow.adept2.util.locking.ReentrantLock.Lock
- Direct Known Subclasses:
ReentrantLock.ReadLock,ReentrantLock.WriteLock
- Enclosing class:
- ReentrantLock<T>
Represents a reentrant exclusive lock which allows only one object to hold
the lock but it may hold it multiple times.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanWhether this lock has been invalidated and no thread should wait any more.protected final LockCountManager<? super T>The manager for counting the lock acquisitions of an object and encapsulating whether objects are equal.protected final AtomicIntegerCounts how many threads are waiting to get this lock. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedLock(LockCountManager<? super T> lockCountManager) Creates a new exclusive lock using the designatedLockCountManagerfor comparing objects. -
Method Summary
Modifier and TypeMethodDescriptionRetrieves a string representation of the object that is currently holding the lock.booleanDetermines if the designated object holds this lock.voidInvalidates 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.voidProvides the designated object with an exclusive lock.voidProvides the designated object with an exclusive lock.protected booleanGets whether a notification of waiting threads is required.intReturns the number of objects waiting to acquire this lock.booleanReleases this lock for the designated object if it holds the lock.
-
Field Details
-
lockCountManager
The manager for counting the lock acquisitions of an object and encapsulating whether objects are equal. -
waitersForLock
Counts how many threads are waiting to get this lock. -
invalidated
protected boolean invalidatedWhether this lock has been invalidated and no thread should wait any more.
-
-
Constructor Details
-
Lock
Creates a new exclusive lock using the designatedLockCountManagerfor comparing objects.- Parameters:
lockCountManager- The manager counting locks for objects.
-
-
Method Details
-
lock
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
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
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
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
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.
-