• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
An elephant? An actual elephant. Into the apartment. How is the floor still here. Hold this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic