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

my lock. notifyall() question

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
lock(int record)
{
synchronised(vector)
{
if record in vector then wait()
else
vecotr.add(record)
}
}

unlock(int record)
{
synchronised(vector)
{
vector.remove(record)
notify() or do i notifyall()???
}
}
My question is do i notify or notifyall()???
if i notify it will tell only 1 waiting thread that it can try and lock. if it goes back to waiting, then no other threads will be told, therefore do i have to use notifyall() to give every thread a chance?
Cheers,
Joe
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that you are calling wait()/notify()/notifyAll() on the vector object. Think about it for a second...
Client A locks record 1.
Client B locks record 2.
Client C tries to lock record 1 and waits...
Client D tries to lock record 2 and waits...
Now supose client A unlocks record 1.
if you use notify() instead of notifyAll(), only one waiting thread will be notified. If Client D is notified, for example, it will check and see that record 2 is still locked by another client, and will wait. So Client C, who wanted to get a lock on record 1 will continue to wait indeterminately. You should better use notifyAll(). What might happen is that many threads will be notified, and most of them will go to sleep back again...
Hope this helps!!!
Benjam�n...
[This message has been edited by Benjam�n Amodio (edited May 09, 2001).]
 
J Hartley
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, the reason why I was unsure was because letting every thread check its lock seemed a bit inefficient. But ill go with my initial design and use notifyall.
Thanks For Yuor Help
Joe
 
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic