java.lang.Object
org.ematgine.utils.concurrent.WriterPreferenceReadWriteLock
org.ematgine.utils.concurrent.ReentrantWriterPreferenceReadWriteLock
- All Implemented Interfaces:
- ReadWriteLock
- public class ReentrantWriterPreferenceReadWriteLock
- extends WriterPreferenceReadWriteLock
A writer-preference ReadWriteLock that allows writers to reacquire
read or write locks in the style of a ReentrantLock.
Readers are not allowed until all write locks held by
the writing thread have been released.
Readers may also reacquire read locks, but not write locks.
Among other applications, reentrancy can be useful when
write locks are held during calls or callbacks to methods that perform
reads under read locks.
Sample usage. Here is a code sketch showing how to exploit
reentrancy to perform lock downgrading after updating a cache:
class CachedData {
Object data;
volatile boolean cacheValid;
ReentrantWriterPreferenceReadWriteLock rwl = ...
void processCachedData() {
rwl.readLock().acquire();
if (!cacheValid) {
// upgrade lock:
rwl.readLock().release(); // must release first to obtain writelock
rwl.writeLock().acquire();
if (!cacheValid) { // recheck
data = ...
cacheValid = true;
}
// downgrade lock
rwl.readLock().acquire(); // reacquire read without giving up lock
rwl.writeLock().release(); // release write, still hold read
}
use(data);
rwl.readLock().release();
}
}
[ Introduction to this package. ]
|
Field Summary |
protected long |
writeHolds_
Number of number of acquires on write lock by activeWriter_ thread |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
writeHolds_
protected long writeHolds_
- Number of number of acquires on write lock by activeWriter_ thread
ReentrantWriterPreferenceReadWriteLock
public ReentrantWriterPreferenceReadWriteLock()
allowReader
protected boolean allowReader()
- Description copied from class:
WriterPreferenceReadWriteLock
- Override this method to change to reader preference
- Overrides:
allowReader in class WriterPreferenceReadWriteLock
startWrite
protected boolean startWrite()
- Overrides:
startWrite in class WriterPreferenceReadWriteLock
endRead
protected WriterPreferenceReadWriteLock.Signaller endRead()
- Description copied from class:
WriterPreferenceReadWriteLock
- Called upon termination of a read.
Returns the object to signal to wake up a waiter, or null if no such
- Overrides:
endRead in class WriterPreferenceReadWriteLock
endWrite
protected WriterPreferenceReadWriteLock.Signaller endWrite()
- Description copied from class:
WriterPreferenceReadWriteLock
- Called upon termination of a write.
Returns the object to signal to wake up a waiter, or null if no such
- Overrides:
endWrite in class WriterPreferenceReadWriteLock