File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes how many locks to use? 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 "how many locks to use?" Watch "how many locks to use?" New topic
Author

how many locks to use?

Jonathan Elkharrat
Ranch Hand

Joined: Dec 31, 2008
Posts: 170

if i use "one lock to control them all" i'll waste CPU every notifyAll when i wake up all threads waiting for all records.
if i use one lock per record then i'll need N locks (N is the database size) or conditions (it doesn't matter, it's still an
object after all) because there's no way* to clean up a lock when not needed and at some point N locks would exist.

so in short: CPU vs. RAM, who win?
(or there's no straight answer for that?)

*at least not that i found and is guaranteed AND NOT SUPER COMPLICATED FOR THE ASSESSOR


SCJP 5, SCWCD 5, SCBCD 5
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5266
    
  13

I just used 1 lock in combination with notifyAll, so I might waste some cpu cycles for threads that were waiting for a different record than the one that was unlocked. No big deal


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

Joined: Apr 29, 2011
Posts: 32
yeah originally I had the lock handover situation as described in the SCJD by Monkhouse but it was a bit more complicated than having 1 lock that rules them all, so for sacrificing more CPU cycles for simplicity I went with the latter implementation, note it does state in the assignment that the software should be understandable by a junior programmer which in essence is true to the fact in a real world scenario, every heard of the KISS principle? plus I think with commodity hardware getting faster all the time simplicity can rule in the majority of cases
I am currently doing the SCJD myself just wanted to do an 'unselfish' post for a change & as you see Roel who replied to your post has indeed been quite insightful with posts related to questions i had cheers Roel, ok digression over :P
Jonathan Elkharrat
Ranch Hand

Joined: Dec 31, 2008
Posts: 170

indeed, i'm always amazed to see how devoted are the bartenders here and
how they answer every question with patience and great knowledge..

so --->
for Roel
for Roberto
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5266
    
  13

Thanks guys for these kind words. With each post we try to do the best we can and it's nice to see it's highly appreciated

And of course we hope you guys keep visiting this forum and answer also some questions! So you'll give a little bit back to the community (and it's also much appreciated... by us )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how many locks to use?