File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Is This locking code good enough? 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 "Is This locking code good enough?" Watch "Is This locking code good enough?" New topic

Is This locking code good enough?

Dan Duras

Joined: Feb 26, 2007
Posts: 1
I am preparing for my submit and I want to be sure that I
don't mess up the locking part so I need assurance
does this locking method seem ok or not?
[ February 26, 2007: Message edited by: Barry Gaunt ]
Maciej Miklas
Ranch Hand

Joined: Feb 12, 2007
Posts: 61
your unlock method musst be using notifyAll. This is not super cool, but it will work
Leo Himpth

Joined: Mar 01, 2007
Posts: 2

just a question about autoboxing: why are you explicitly wrapping when getting / setting the values in locks? (i think currently every submission must at least be 5.0 or above)

one nice thing i observed is that you check for locked records not with locks.contains(), but with locks.get() == null. if it was my code, this probably would be worth a comment (e.g. "locks may contain null values").
[ March 02, 2007: Message edited by: Leo Himpth ]
Lucy Hummel
Ranch Hand

Joined: Apr 07, 2005
Posts: 232

In Dan's solution there is no notifyAll() in the method lock().

I checked several books for example where to put notify() and notifyAll().

I found both calls only in the code similar to our unlock(). So in methods that do not contain a waiting-loop.

Since I have not found any code snipplet that provides a notifyAll() in the waiting-loop, I also will not put it there. So the notifyAll() is just in my unlock() method.

What are your opinions on that issue?

Have you anything like a waiting-loop that has a notifyAll() at its end?

Looking forward to your answers.

----------------------------------<br />| SCJP, SCWCD, SCBCD, SCEA, SCJD |<br />----------------------------------
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 918

Hi Dan,

1.I presume that you don't have a separate class for locking - I base this presumption on the fact that you use methods like prepareFileForRead or validateInput in the lock method.
Tip : use a separate class for the lock manager - is easy to maintain.
2.Don't use flag values (value to indicate an anormal method termination), I refere here on the "-1"
Tip: try to use exceptions to signal this.

Regards M
[ April 08, 2007: Message edited by: Mihai Radulescu ]

Tai Hu

Joined: Apr 27, 2007
Posts: 9
I think for each place you used wait(), you need to call notifyAll() at the end. Otherwise there is possible that waiting thread will not be waken up forever. For instance, you have 2 threads are waiting for a record becoming unlock. However, all the active threads (threads called lock method but not called unlock methods yet) are crashed before calling unlock() methods. Then your 2 waiting threads will be waiting forever.
I agree. Here's the link:
subject: Is This locking code good enough?
It's not a secret anymore!