• Post Reply Bookmark Topic Watch Topic
  • New Topic

No notify for a waiting thread

 
sandeep jaiswal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can any body tell me what happens to the thread, who goes to waiting state because of wait() call, and never get a notification because no one calls notify() or notifyAll()??

Thanks
Sandeep Jaiswal
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may wait forever. It may be woken up by an interrupt() call. Very occasionally it may spontaneously wake up all by itself, if nothing else is holding the lock.
 
sandeep jaiswal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your reply.

I know nothing is guaranteed when in comes to thread.
But my query is :

If no one calls Notify, No interrupt() then in what basis this will wake itself.
Any algorithms placed in?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All the information is on the javadoc page for java.lang.Object:


A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied.


There's no algorithm, and it's not predictable. It just happens sometimes.
 
sandeep jaiswal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, many people are surprised by this! But it isn't a Java invention; it's a feature of standard thread libraries, like POSIX threads, that JVMs are built on. For example, read this blog entry which explains where spurious wakeups come from on Linux.
 
sandeep jaiswal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.
That was indeed, put lots of light:
Now I can Go ahead in some direction in search of more on this
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!