reentrantreadwritelock java
class CachedData {
Object data;
volatile boolean cacheValid;
final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
void processCachedData() {
rwl.readLock().lock();
if (!cacheValid) {
// Must release read lock before acquiring write lock
rwl.readLock().unlock();
rwl.writeLock().lock();
try {
// Recheck state because another thread might have
// acquired write lock and changed state before we did.
if (!cacheValid) {
data = ...
cacheValid = true;
}
// Downgrade by acquiring read lock before releasing write lock
rwl.readLock().lock();
} finally {
rwl.writeLock().unlock(); // Unlock write, still hold read
}
}
try {
use(data);
} finally {
rwl.readLock().unlock();
}
}
}
Also in Java:
- remove item from arraylist in java
- loop while in java
- make a commet in java
- dicom read with java
- how to check if a string contains only alphabets and space in java
- how to remove components from a JFRame java
- java list all non directory files in the directory
- number to char java
- stack class in java
- when super add by constructor in java
- Don't use a line-beased input after a token-based input.
- length of string java
- converter int array para string java
- query java persistence
- remove last character from string java
- java jaxb unmarshall xml to map
- how to know when user is done typing android
- JavaFX onMouseEntered
- java set example
- jsonobject to java object
- java null pointer exception
- java script dynamic varibale creation
- how to add an object to a list of objects in java
- boolean parse jtextfield