File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Lock for whole object? or only synchronized block? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Lock for whole object? or only synchronized block? " Watch "Lock for whole object? or only synchronized block? " New topic
Author

Lock for whole object? or only synchronized block?

m ali
Ranch Hand

Joined: Apr 12, 2007
Posts: 49
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

Joined: Dec 11, 2006
Posts: 66
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.


Ciao,<br /> Tommaso<br /> <br />~*~*~*~<br />There are 10 types of people, those who understand binary and those who don't.
m ali
Ranch Hand

Joined: Apr 12, 2007
Posts: 49
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

Joined: Dec 11, 2006
Posts: 66
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

Joined: Apr 12, 2007
Posts: 49
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

Joined: Mar 20, 2007
Posts: 135
Nice example Tommaso!!!


SCJP 1.5, SCWCD 1.4, Next... SCBCD5
Tommaso Nuccio
Ranch Hand

Joined: Dec 11, 2006
Posts: 66
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

Joined: Jan 05, 2007
Posts: 98

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

Joined: Mar 18, 2007
Posts: 4
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Lock for whole object? or only synchronized block?
 
Similar Threads
Synchronization
Syncronizing variables or classes - Exercise 9-2
Does a synchronized method lock on the whole object or just on his own method?
Synchronizing Code
Locks