laurel: starts running
laurel: going to sleep
hardy: starts running
hardy: going to wait for laurel
laurel: out from sleep
laurel: is exiting run
hardy: IS OUT FROM WAIT ON LAUREL.
hardy: is exiting run
Can any one please explain how the thread 'hardy' came-out from the waiting and who notified on the synchronized object 'laurel'? Please note that there is no laurel.notify() statement anywhere in the code-snippet.
Also if I comment-out the try-catch including the Thread.sleep, hardy waits forever, though laurel completes its execution.
Rajanand P K, Oracle Certified Professional, Java SE 6 Programmer.
if Thread A is waiting on Thread B object, then Thread A will be notified when Thread B completes...
So, JLS treats this an exceptional case for the thread being used as a monitor, right? Because, theoretically speaking, if there is an object1.wait() from ThreadA, ThreadA would wait until there comes a object1.notify() from another thread which again synchronized on object1. And, in our case, we do not have an explicit notify call at all.
You cannot rely on this behavior. This happens because Thread.join has been implemented this way but JLS doesn't specify this behavior. So this behavior might change in the future thus it will not be asked in SCJP exam ...