• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Hashmap + Reentrant Locks

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic