Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Hashmap + Reentrant Locks  RSS feed

 
Michael Vargenstien
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm trying to understand the behavior of the Reentrant locks classes when used in conjunction with Hashmap. I'm attempting to use the put() method within a lock.writeLock().lock(). So for example:

code starts here
.......
lock.writeLock().lock();
....
someHashmap.put(myValue, yourValue);
...
..
finally {
lock.writeLock().unlock();
}

The write lock is actually used to protect concurrent access to a database.

Also, any good tutorials on this stuff? I've ordered the concurrency in practice book but I'm still waiting for its arrival.

Thanks!
 
Henry Wong
author
Sheriff
Posts: 22841
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reader Writer locks are straightforward. You get either the read lock or the write lock, then you lock it during the section of code where you don't want to be interferred with (ie. no concurrency access to protected store).

In the case of the read lock, you are guaranteed that only threads that also tried to get the read lock could be running. Threads that tries to get the write lock will be blocked.

In the case of the write lock, you are guaranteed that all threads that tries to get the read or write lock would be blocked. You are the only thread that owns the lock.

Of course, this is all cooperative. All threads -- whether reading or writing -- should use the appropiate lock. Another issue to worry about is whether the store will work with concurrent reads. It is probably safe to assume that concurrent reads to a DB should work, but you should confirm it.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!