E
easy
fomr javaoc of ReentrantReadWriteLock
an example:
class CachedData {
Object data;
volatile boolean cacheValid;
ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
void processCachedData() {
rwl.readLock().lock();
if (!cacheValid) {
.....
use(data);
rwl.readLock().unlock();
}
}
Using volatile here makes sense to me.
but if in my class looks like this,
class CachedData {
HashMap<Key, Obj> data; // <---- here
ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
void processCachedData(Key k) {
rwl.readLock().lock();
if (!data.containsKey(k)) { // <--- here
.....
use(data);
rwl.readLock().unlock();
}
}
should I declare as
"volatile HashMap"
or for which "variable" type should I apply volatile in such
situation?
thanks.
an example:
class CachedData {
Object data;
volatile boolean cacheValid;
ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
void processCachedData() {
rwl.readLock().lock();
if (!cacheValid) {
.....
use(data);
rwl.readLock().unlock();
}
}
Using volatile here makes sense to me.
but if in my class looks like this,
class CachedData {
HashMap<Key, Obj> data; // <---- here
ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
void processCachedData(Key k) {
rwl.readLock().lock();
if (!data.containsKey(k)) { // <--- here
.....
use(data);
rwl.readLock().unlock();
}
}
should I declare as
"volatile HashMap"
or for which "variable" type should I apply volatile in such
situation?
thanks.