Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread.wait() doubts.......  RSS feed

 
Vinney Shanmugam
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • 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???
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • 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 ]
 
Don Solomon
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • 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.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!