Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

readLock vs writeLock

 
Matt Pavlovich
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I'm trying to finish up my locking functionality, and am a little stumped as to whether or not I can use readLock on a comparison statement, or if I should just go with writeLock. Here is the code:



The mapCookie value is set before hand by calling the lock() (which uses writeLock()), and the lockCookie value is a local variable in a method which is not synchronized. My understanding is that the readLock() will not attempt to read any value which is being modified, but since I am comparing two values, rather than just reading them, I wasn't really sure if this comparison qualifies as an operation needing the writeLock() or not.

Thanks to everyone for your help.
Matt
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't use readLock and writeLock, so I didn't have to handle this kind of questions, just using my common sense (the little bit I have ) This question pops immediately into my head after reading your post: is it ok that the map containing the cookies is changed (new cookie added, existing cookie removed,...) while I'm trying to retrieve a cookie from this map to compare with another cookie? If it is, you can go for a readLock; else you should use a writeLock.
 
Matt Pavlovich
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel,

My guess is you have more than a little bit. ;) So the real question is, can my map be updated by one thread, and read by another? I used writeLock around the logic which contains the code that adds a record to the map (if the record is not already in there) and then returns the long associated with the key. So, technically, the map cannot be read if it is being written to, and cannot be written to if it is being read, which means I should be okay on that end. In other words, the way in which I get my mapCookie value is thread safe. However, the local variable (long lockCookie) is the real issue I guess. It could change if another thread calls its containing method. So, the question is now, does the readLock() method check to make sure a local variable is not being written to and vice versa? My guess is that it does, and that readLock is fine in this instance, but now that is just my common sense working. What do you think?

Thanks,
Matt
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty sure your local variable is not an issue at all Because as far as I know local variables are always (inherently) thread-safe. You'll can find a thread discussing thread-safety and local variables here.
 
Matt Pavlovich
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel,

Thanks for the info and for the link. Much obliged!

Matt
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic