This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
its written in k&b that the this object gets locked..in this case which is getting locked? if t starts off first then is it going to lock the tl object?what about ty??will it be able to execute?or will it be blocked?? sorry not very clear about the concept..please help me understand this..
When thread t's synchronized method starts, tl will be locked. to will wait for tl's lock which will be free only when t's synchronized method is finished.
Also notice, the loop is not in synchronized block or method. Both threads (t & to) will start and the same object tl will be used by both inside the loop. Order of processing is NOT guaranteed. But only one thread will be incrementing (executing increment()) at a given time on object tl.
thread ty is not affected by this as its acting on altogether different object.