I have a great confusion, whether a thread running on a synchronized block or method. Its get the lock on whole object? or its get lock on synchronized block/method. Please explain anybody. Is there any possibility to have an object multiple locks? please clarify?
there is only one lock per object. This makes sense, because if a Thread picks up the lock, no other thread can access the object. And that's what it is all about.
A thread can get more than one lock, i.e. two locks on two different objects. It enters a synchronized method and gets the lock on an object and within this method it maybe access another synchronized method of a different objects and gets that lock also.
If you have a synchronized block in a static method, the lock is for the whole class, because static methods are class methods, whereas non-static methods are specific to an instance of a class.
Ciao,<br /> Tommaso<br /> <br />~*~*~*~<br />There are 10 types of people, those who understand binary and those who don't.
Joined: Apr 12, 2007
If a thread accessing a synchronized method, its get the object lock. Its ok, got it. But at this time is any other thread, say thread-2 able to access the same object's another synchronized or non-synchronized methods? If yes, is the thread-2 holds gets the object lock? its owned by previous thread-1. Please clarify.
Thanks in Advance.
Joined: Dec 11, 2006
Hi m(are you the one from James Bond ),
I give you two explanations: 1 - only one lock per object. 2 - "synchronized" tells a thread, "You can only enter, if you have the lock on the object!"
Question: Can another thread get the lock, when trying to access another synchronzed method?
Example: Imagine a house with 5 rooms, 3 of them have a door which can be locked with a key and are per default locked. The housekeeper, Mr Scheduler, has only one key and he is the only one to give it to someone. Now we have three guys in the house and all really need to go for a pee The bathroom is one of these three locked rooms. So Mr Scheduler decides to give the key to the guy, who currently has the highest priority. That is Peter, because he has been drinking water the whole day and really needs to ... Peter gets the key, enters the bathroom and locks the door. The second guy is hungry and wants to enter the kitchen, that is also locked. But the key is with Peter in the bathroom! How can he enter the kitchen?
Joined: Apr 12, 2007
Thanks Tommaso Nuccio,
I got it. When a thread gets the lock on an object, then no other threads have access to the same object's other synchronized blocks/methods. Is it applicable to non-synchornized methods.
when a thread gets a lock of an object,means that it executes ANY of the object's synchrinized methods.During that time,no other thread can ececute any of the object's synchronized methods until the first ones finishes executing it's synchronized code,but any other thread can execute any of the object's non synchronized methods(non synchronized methods don't depend on locks).Hope this helps Dimitris