File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wait and Notify concept

 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey All,
I am having some problem in using Wait and Notify, Please let me know where i am doing wrong

here is the program:


When i run the program above, i guess it goes into deadlock state. i would like to know, why is this happening?

Thanks
 
Peter Chase
Ranch Hand
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not totally clear what behaviour you are attempting to implement. Can you explain?

I can see that the program may well hang. Although you have two notify() and two wait() calls, you have not guaranteed that anyone is waiting when you do your notifications. Therefore, one or both notify() calls may happen when there is no wait() in progress. Then, one or more of the wait() calls will wait forever in vain.

A final point. This situation is not "deadlock". Deadlock is where two threads are trying to acquire one of two resources, where one thread already holds one of the resources and the other thread holds the other resource. In this situation, the threads will be stuck forever trying to enter the synchronized block.
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah u right
Even i dont know what i am trying to implement, i just thought of writing some piece of code using Wait and Notify, got this problem during running,
well, i think now i have got to know what the problem is

Thanks for ur reply
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes it's good to just code and try the virtual equivalent of "what does that button do"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic