Hi,
I was going through the
Thread Self
Test Question #12 in Page 767 given in the "Kathy Sierra & Bert Bates"
SCJP 6 Study Guide and I quite understood it. Then I modified the question as is given below. I made the laurel.wait() to synchronize on laurel. Then what I see is, hardy waits and continues, once the laurel came out from the sleep after one second.
Does it mean that laurel obtains a lock on itself and notifies hardy once it releases the lock, though there was no synchronized keyword used within the laurel run method? My understanding was that, a 'wait' can continue only upon a 'notification' which must be through an explicit 'notify' invocation and sleep would never notify.
The reason why I ask is, if I comment-out the sleep code within laurel(including its exception handling), then the synchronized wait in hardy waits forever.
A reply is much appreciated.
The modified code snippet is given below in the bold font.
The output obtained was:-
A <- from laurel
D <- from hardy, then hardy waits
C <- from laurel after 1000 milli seconds
F <- from hardy, out from the waiting
If 'sleep' from laurel is removed including the try-catch, output is
A <- from laurel
C <- from laurel
D <- from hardy, then hardy waits indefinitely.
program waits indefinitely.
Thanks
Raj