Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Lock for whole object? or only synchronized block?

 
m ali
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchars,

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?

Thanks in advance.
 
Tommaso Nuccio
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

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.

Good luck.
 
m ali
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tommaso,

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.
 
Tommaso Nuccio
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Good luck.
 
m ali
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks.
 
Damodar Mukherjee
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice example Tommaso!!!
 
Tommaso Nuccio
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

well the example is not too elaborated, because Mr Scheduler can have more than one key, one per instance, but you get the idea.

Good luck.
 
Arad Chear
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.


no

non-synchronized method can invoke from many threads at same time , even if the instance lock was taken by one thread
[ April 20, 2007: Message edited by: Eisa Ayed ]
 
Dimitris Karagiannakis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic