• 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

Thread waiting and notifying

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Supposed two threads T1 and T2 both call wait() on object A. Then object A calls notify() and the JVM chooses one of the threads to be notified - let us say it chose T1. If A never calls notify() again, does T2 just sit around a wait forever even though A's lock may become available?
Thanks!
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. This is why, if you use notify() rather than notifyAll(), it is important to make sure that every thread the might be notified will call notify() again when it's done with whatever it needed to do. Otherwise, one of your waiting threads can just sit there forever.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in order to call a wait() on an object the thread has to own the lock of that object , now how can 2 threads get the lock on the same object simultaneously
Plz expain
Thanx
Sonu.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sonu
Let say there are two threads A and B. Thread A acquires a lock and then because of a call wait call goes in the waiting state. Then again the same thing happens to Thread B. Now the lock is neither with thread A or B. The wait() method releases the locks.
[ June 30, 2003: Message edited by: Anupam Sinha ]
 
sonu sharma
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ohhh shucks i posted something so silly
but thanx for clearing it up Anupam.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic