aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes reserveDVD() one ReentrantLock or a List of ReentrantLocks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "reserveDVD() one ReentrantLock or a List of ReentrantLocks" Watch "reserveDVD() one ReentrantLock or a List of ReentrantLocks" New topic
Author

reserveDVD() one ReentrantLock or a List of ReentrantLocks

Bart Lamberigts
Greenhorn

Joined: Jun 21, 2010
Posts: 1
Hi,

for implementing locking for update/release, the book (SCJD Exam with J2SE 5) uses one ReentrantLock() object, and a Condition (lockReleased) (see book pg 154).

However, my solution is different, and I wanted to know if this is better/worse...


I have a list of Lock objects, one for each 'DVD'.

my method looks like this:



I unit tested my solution, and it seems to work. The only concern I currently have is that such a list of Locks could take a big chunk of RAM memory (??)

regards,

Bart

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5407
    
  13

Hi Bart,

Seems fine by me, but I didn't use the new concurrency api. I coded my Data class using synchronized, wait and notifyAll.

I think the main question you have to ask yourself why you would opt for this approach, because you have to justify your decision in the choices.txt file. And indeed if your database has 10000 'DVDs', you will need a lot of RAM memory. This might be a drawback that you have to consider and address in this file. I for example used a record cache, which can have a memory issue too. So I documented that decision and proposed some alternatives/solutions to solve this memory issue.

And based on your name I would guess you are from Belgium too (could be the netherlands too)

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Raf Szczypiorski
Ranch Hand

Joined: Aug 21, 2008
Posts: 383
I would see in this approach that it has the advantage that each row can be acquired separately and independently of other clients, whereas with single synchronized with waits / notifyAlls you are locking the whole Data class from other clients. When coded correctly, when there are thousands concurrent users, that lock and unlock like fierce beasts, this could have huge performance advantage. You could even go further and use read / write locks for lock map readers and writers probably.
Ram is cheap, user experience is not. Still, such access to the lock map / collection has to be synchronized itself.
I didn't even think of that approach, I opted for the simplest one (just like Roels), thanks for your post.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reserveDVD() one ReentrantLock or a List of ReentrantLocks