Hi If there are more than one thread waiting to own the monitor of that object, which one will be notified first when notify() method is encountered? Is it the first thread that called wait() for that monitor or is it arbitarly chosen? Thanks Raji
you can get detailed explanation in page 695 of Java How To Program by Deitel&Deitel. when a running thread calls wait the thread enter waiting state where it waits in a que associated with the particular object on which wait was called . The first thread in the wait queue for a particular object becomes ready on a call to notify issued by another thread associated with the object. in more than one thread is in queue then it is better to call notifyAll method.
If there are multiple threads waiting on a lock, there is no guarantee what thread will run after a call to notify() or notifyAll(). It will NOT necessarily be the one with the highest priority, or the one that has been waiting the longest. notify() will wake up one thread, you have no control over which one, unless you implement the Specific Notification Pattern found here. notifyAll() wakes up all threads, but only one gets the lock, the others block. Again, you can't control this unless you implement the above pattern. For more on this, see my post here. Peter Haggar ------------------ Senior Software Engineer, IBM author of: Practical Java