Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

do I need "volatile" for HashMap? when I apply ReentrantReadWriteLock on it.

Thread view: 
easy - 30 Sep 2007 12:44 GMT
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.
Knute Johnson - 30 Sep 2007 17:53 GMT
> fomr javaoc of ReentrantReadWriteLock
> an example:
[quoted text clipped - 12 lines]
> }
> Using volatile here makes sense to me.

booleans used for loop control and things like that need to be volatile
only if accessed from more than one thread.  In your example above that
isn't clear.  In either case if your boolean were used in a synchronized
block or as in this case a locked section of code it would not need it's
own synchronization to make changes in other threads visible.

> but if in my class looks like this,
> class CachedData {
[quoted text clipped - 12 lines]
> should I declare as
> "volatile HashMap"

No.

> or for which "variable" type should I apply volatile in such
> situation?
>
> thanks.

volatile is used mainly to ensure multi-thread visibility.  If you are
going to change the value of the variable in another thread and want to
see those changes then make the variable volatile.  But as I said above
if the variable is only accessed inside a synchronized block or some
other synchronization code (eg ReentrantReadWriteLock) then all
variables are made visible to this thread.  It is a freebie side effect.

You might want to invest a few bucks in the book "JAVA Concurrency in
Practice", by Brian Goetz.  Very valuable when writing multi-threaded
Java code.

Signature

Knute Johnson
email s/nospam/knute/

easy - 30 Sep 2007 21:41 GMT


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.