Class ReentrantLock.ReadLock
- java.lang.Object
-
- de.aristaflow.adept2.util.locking.ReentrantLock.Lock
-
- de.aristaflow.adept2.util.locking.ReentrantLock.ReadLock
-
- Enclosing class:
- ReentrantLock<T>
protected class ReentrantLock.ReadLock extends ReentrantLock.Lock
Represents a read lock which allows multiple objects to hold the lock simultaneously and multiple times but blocks as soon as one object wants a write lock.
-
-
Field Summary
-
Fields inherited from class de.aristaflow.adept2.util.locking.ReentrantLock.Lock
invalidated, lockCountManager, waitersForLock
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
ReadLock(LockCountManager<? super T> lockCountManager)
Creates a new read lock using the designatedLockCountManager
for comparing objects.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
lock(T object, long timeout)
Provides the designated object with a read lock.protected boolean
notificationRequired()
Gets whether a notification of waiting threads is required.boolean
unlock(T object)
Additionally notify the writers waiting for readers on the write lock.-
Methods inherited from class de.aristaflow.adept2.util.locking.ReentrantLock.Lock
currentLockingObject, hasLock, invalidate, isLocked, lock, objectsWaitingToLock
-
-
-
-
Constructor Detail
-
ReadLock
protected ReadLock(LockCountManager<? super T> lockCountManager)
Creates a new read lock using the designatedLockCountManager
for comparing objects.- Parameters:
lockCountManager
- The manager counting locks for objects.
-
-
Method Detail
-
lock
public void lock(T object, long timeout) throws InterruptedException, TimeoutException
Provides the designated object with a read lock. The current thread will be blocked until the lock can be acquired or the timeout elapsed. Writers have priority, that is as soon as there is one object requesting a write lock, the acquisition of reading locks will be blocked.Since the lock is reentrant, an object may acquire a lock multiple times. But it also has to release the lock per acquisition afterwards.
- Overrides:
lock
in classReentrantLock.Lock
- Parameters:
object
- The object that requests a read 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, anInterruptedException
will be thrown.TimeoutException
- If the designated timeout elapsed before the lock can be acquired, aTimeoutException
will be thrown.
-
unlock
public boolean unlock(T object)
Additionally notify the writers waiting for readers on the write lock.- Overrides:
unlock
in classReentrantLock.Lock
- 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)
false
will be returned.
-
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. This also respects writers that wait for the read lock.- Overrides:
notificationRequired
in classReentrantLock.Lock
- Returns:
- Whether a notification of waiting threads is required (no more locks and there are waiters for the lock).
-
-