Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

my lock. notifyall() question

 
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
 
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
 
This is my favorite show. And this is my favorite tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic