Hi Henry,
ahhh..right i completely overlooked that the code was running in an infinite loop !
But it still doesn't make it any clear to me as to why certain notifications weren't caught by the waiting thread !!
I was under the impression that :
1)When a synchronized block is executing (by holding the lock on a third party object) no other object can enter that block or acquire the object's lock. Also when one wants to call wait/notify on an object it needs to acquire the object's lock. So according to my understanding the waiting thread calls wait and releases the object lock so other threads can use it.
Now the notifying thread acquires the lock performs certain operations inside the synchronized block and calls notify. As soon as it exits the synchronized block it releases the object lock.
Next the waiting thread comes out from the blocking state ,acquires the object lock and performs the necessary operations. Meanwhile ,if the notifying thread wants to enter the synchronized block again it will require the object lock !!
So,i thought the expected and actual output would match.
I am not able to figure out what i am missing.
I understood why you pointed out the following to me :
The notify() method doesn't trigger the wakeup, context switch, etc.... all it does is send the notification to a waiting thread.
and that's because my following piece of code was misleading:
I should have written that in the waiting thread.
as
Your second point is also understood
I didn't understand the condition you were talking about. I really didn't feel that there should be condition which will monitor the wait/notify events. Please clarify if i am missing anything.
Your waitandnotifytest thread just sends the notification, and does so blindly in a loop (no flag to confirm receipt, or that a thread is waiting)... see point one and two. This causes a bunch of missed notifications.
Why does it cause notifications to be missed ?
Thanks,
Adithya.