Tn Sharma wrote:
And what if somehow notify() is called before wait() .then waiting will infinite?
Who is calling wait()? Who is calling notify()? There are multiple use-cases in context with this topic, so, which one do you mean?
Anyway...
In the case of your original example (ie. your thread calling wait(), and the clean up code for the thread calling notifyAll()). ... It is not possible for your thread to call wait() after the clean up code for your thread to call notifyAll(). As long as your thread is running, the environment can't clean up after it.
In the case the core library implementation of the join() method (ie. the thread that called join() calling wait(), and the clean up code for the thread calling notifyAll()). ... It is not possible for wait() to be called by join() after the clean up call. The implementation of join() does not do a blind wait. It will check to confirm that the thread is still alive before calling wait().
In the general case (ie. general question -- and not related to this topic). ... Notifications that are sent, when there are no threads waiting for it, are simply discarded.
Henry