Thanks,<br />Mike
SCJP,SCJD,SCWCD,SCBCD,SCDJWS,SCEA
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Thanks,<br />Mike
SCJP,SCJD,SCWCD,SCBCD,SCDJWS,SCEA
Before a thread execute unlock method, the thread obtains a lock on this object. If the thread doesn't call notifyAll(), then other threads don't know that this lock is available.
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
It looks like you are confusing the mutual exclusion (mutex) lock on the object used by the synchronized blocks with the object holding our conceptual locks for our applications.
As soon as one thread has control of the mutex, no other threads can enter the synchronized block. However as soon as the first thread exits the synchronized block or calls wait() it releases control of the mutex, and any other thread that wants to use the mutex can do so. There is no need for a call to notifyAll() for another thread to get control of the mutex.
Our call to wait() has nothing to do with controlling access to the mutex. The thread that calls wait() is simply waiting until the lock in the conceptual lock object that it wants is removed. When we remove the conceptual lock, we will call notifyAll() in order to tell the waiting thread that it can try to get the conceptual lock again.
Note that when we call notify() or notifyAll() we are not telling all the waiting threads that the mutex is now available. When they wake up, they have to try and get the mutex themselves before they can attempt to create the logical lock.
Thanks,<br />Mike
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog