Originally posted by poorna k:
I have a few doubts about this example you have given.
As per my understanding , wait() and notify() methods are available to all objects,and is meaningfully used when you are synchronizing an object.
The wait() method defined for the object being synchronized will act on the current thread,and cause it to release its lock on the object.
The notify() method on the other hand,causes the threads in the waiting pool to get notified about the imminent release of the object lock held by the current thread.
Rob, as per the above understanding
I feel that the corral is similar to a waiting pool for the threads(cows here ) to get a lock of the object(the gate to exit the corral).
When a cow is currently in the process of exiting the gate(that is it holds lock on the object),if it executes the wait() method defined there,the current thread /cow releases the lock and comes back into the corral,giving up the gate.
If on the other hand a cow /thread is on the way out,and is going to release the lock (no longer using the gate ),the notify() or notifyAll() method is called on the cows still within the corral,and the thread scheduler / ranch hand selects a cow to go out the gate.So this lucky cow gets the key to the gate.
Is this correct??