posted 16 years ago
Hey Sunny,
The problem is that you are incrementing the counter before getting into wait() (compounding the problem you do it before getting into synchronized block.) So, the counter does not really tell the number of threads waiting on the mutex. Thus, the notifyAll() in many cases will be called even before all the threads have gone into the waiting state and thus, the threads getting into wait() after notifyAll() will be stuck forever and never get a notify call.
A simple soultion can be to keep on sending notification till all the threads have received it. This would mean that the counter will be incremented after return from wait and the notification thread keeps on notifying till the counter is 5 !!!
(No points in telling that this is the most stupid way of handling wait-notify! But the logic you have is not something that a real system will have.)