Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is This locking code good enough?

 
Dan Duras
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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?
Thanks
[ February 26, 2007: Message edited by: Barry Gaunt ]
 
Maciej Miklas
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your unlock method musst be using notifyAll. This is not super cool, but it will work
 
Leo Himpth
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

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
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic