permaculture playing cards
The moose likes Threads and Synchronization and the fly likes Wait and Notify concept Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Wait and Notify concept" Watch "Wait and Notify concept" New topic

Wait and Notify concept

Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
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?

Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
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

Joined: Oct 12, 2000
Posts: 5093
Sometimes it's good to just code and try the virtual equivalent of "what does that button do"

I agree. Here's the link:
subject: Wait and Notify concept
It's not a secret anymore!