• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

my lock. notifyall() question

 
J Hartley
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • 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
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic