This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Hand-over-hand locking not needed in SCJD book example ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Hand-over-hand locking not needed in SCJD book example ?" Watch "Hand-over-hand locking not needed in SCJD book example ?" New topic
Author

Hand-over-hand locking not needed in SCJD book example ?

Ronald Wouters
Ranch Hand

Joined: Apr 28, 2005
Posts: 190
Hi all,

I am a bit confused about the example for the "Multiple Notification Objects" in
Andrews book. The author starts out by stating

Under JDK 5 you can have all your threads obtain a lock on the same object but use different Conditions upon which they should be notified.


However the example itself (Listing 5-3. A Less CPU-Intensive reserveDvd Method), uses one ReentrantLock called "masterLock" but it also uses one additional ReentrantLock/Condition pair called "dvdLock" for each dvd !

I have re-written this example to use ONLY the masterlock Lock and multiple Conditions attached to the masterlock, one Condition for each dvd.
My "simplified" example does not track the owner but could easily be extended to do this. You can see the code and the output below.

So in my opinion, we don't really this "hand-over-hand" locking.
Please feel free to give your opinion on this.

Regards.

Here is my revised code ...


... and here the test results



Sun Certified Developer for the Java 2 Platform
Sun Certified Enterprise Architect for the Java Platform, Enterprise Edition 5
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11278
    
  59

Hi Ronald,

I do like your code, however as you have noted you are not tracking the current owner of a lock, which is needed for the assignments.

The dvdLock object in listing 5-3 is simply an object that contains the current owner and the Condition for that record (with some helper methods for those elements). It is simply an extension of your notifiers collection that keeps all the information in one place.

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hand-over-hand locking not needed in SCJD book example ?
 
Similar Threads
Denny's DVD ReservationsManager - signal()
Thread states, notify, notifyAll, wait
SCJD Exam with J2SE 5, Second Edition
Errors in "SCJD Exam with J2SE 5" book
NX: About data consistent