do I need "volatile" for HashMap? when I apply ReentrantReadWriteLock on it.
- From: easy <easy.lin@xxxxxxxxx>
- Date: Sun, 30 Sep 2007 11:44:49 -0000
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.
.
- Follow-Ups:
- Re: do I need "volatile" for HashMap? when I apply ReentrantReadWriteLock on it.
- From: Knute Johnson
- Re: do I need "volatile" for HashMap? when I apply ReentrantReadWriteLock on it.
- Prev by Date: Re: Pointer vs Reference
- Next by Date: Re: generics and arrays and multi-class collections
- Previous by thread: Singly Linked LIst and Objects Newbie Question
- Next by thread: Re: do I need "volatile" for HashMap? when I apply ReentrantReadWriteLock on it.
- Index(es):