Hi all. In the code below t has a lock on l object when it enters the synchronized method1 at line 22(or at least thats what I think) and then waits at line 25. But how come t2 was able to execute method2 at line 31 which is also a synchronized method? My understanding (based on the K&B book) is that every object in
Java has a built-in lock that only comes into play when the object has synchronized method code. Since there is only one lock per object, if one
thread has picked up the lock, no other thread can enter the synchronized code (which means any synchronized method of that object) until the lock has been released.
However, modifying the code a bit to use sleep in lieu of wait at line 25 does lock the object. How is it that lock works if I use sleep and not for wait?
Thanks.