• 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

Is This locking code good enough?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your unlock method musst be using notifyAll. This is not super cool, but it will work
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic