Ranchers, please help me with this question. It would be great if anyone explains me the flow. How can we say that the lock of the object is with the mail thread?? What does synchronized(myrunnable) mean...and where the thread t1 waits?? Does it wait once it enters the synchronized block??on what are we locking??? I fail to understand the flow.
Thanks in advance. [ November 08, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
Main thing to understand here is that 1)sleep() method never releases all the held locks unlike wait() method. So main thread wont release any locks. 2)You are synchronising on same object instance and only one thread can hold lock at a time on same object instance.
So the output would be A,B and C....hopefully iam right
I am not so confidance here, but.. In above code there are two synchronised blocks.So by the concept of Object locking.If a thread enters in any synchronised block or Synchronised methods then it has to keep first thats object's lock key.And in java there is only one key is allowed, not more than one .So possibility for another thread to enter in that block only when if first thread completes its task and release the lock(avail the key).So First of all s,s,s are pointing to 1,2,3 String type object.But in Sync block these instances are pointing to A,B,C strings so here block type local variables are having higher accessbilty than first.So it is printing ABC
yes, as Ganesh said. Plus: It is guaranteed that the main thread runs first. So he is the first thread that gets the key of the Runnable object (there is only one). So the main thread has to leave its synchronized block first, before Thread t1 can run().
Sleep would not release the key, only wait does that.
Comparing sleep and wait:
prints sleep ABC wait 123
There is no notify in BurkisTest, it's using the wait timeout.