wood burning stoves 2.0
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
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

reserveDVD() one ReentrantLock or a List of ReentrantLocks

Bart Lamberigts

Joined: Jun 21, 2010
Posts: 1

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 (??)



Roel De Nijs

Joined: Jul 19, 2004
Posts: 8358

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,

SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
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
It's not a secret anymore!