• 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.wait() doubts.......

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a thread application which waits for a signal from a third party application and based on the notification from it, my thread will come out of wait and continue it's processing. I am running into a situation where, the thrid party application is not sending the notification to my thread, because of which my threads keeps on waiting and hangs.

I want to know, is there any way to kill my thread which waits for the notification after a certain time?

can a thread come out of wait() without notify()/notifyall() being called???
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does wait(long timeout, int nanos) sounds interesting to you?

However, you would not be able to know whether the wait has timedout or the thread has woke up due to a valid notification.
You would additionally have to setup some flag somewhere to indicate whether it was a valid notify or not. (This you would probably have as it is always advisable to wait() in a loop checking for a condition)
[ October 14, 2008: Message edited by: Nitesh Kant ]
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just because a thread calls notify() doesn't mean your wait() thread will be release. the synchronized block where the notify() is invoked must be exited.
 
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[vinod]: can a thread come out of wait() without notify()/notifyall() being called???

Yes, this is at least theoretically possible; it's called a spurious wake-up. Whether this can actually happen or not may depend on your platform. But this is another reason why you should check some external condition (such as a flag) to see if the thing you were waiting for has really occurred, or not, as Nitesh said.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic