File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Class level lock versus Object level lock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Class level lock versus Object level lock" Watch "Class level lock versus Object level lock" New topic
Author

Class level lock versus Object level lock

Anirudh Bhatnagr
Greenhorn

Joined: Sep 01, 2010
Posts: 12
If I make a method static then the thread executing that method would acquire class level lock , now if another thread tries to access some other method of the same class, would it have to wait until the first thread releases the lock on class.

So my question is : Does a class level lock lock the entire class ?
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 511
Hi,
If another thread wants to access same or another STATIC method then it will have to wait for the Class level lock to be released.
So my question is : Does a class level lock lock the entire class ?


Class level lock is required to execute the synchronized methods/blocks those have Static identifier. Static means its the CLASS level method and hence you have to obtain CLASS Level lock.

For synchronized instance methods which are invoked using an object of the class, need a lock on the object to be obtained before its invocation.

Regards,
Amit
Anirudh Bhatnagr
Greenhorn

Joined: Sep 01, 2010
Posts: 12
So does a class level lock stop a thread to acquire Object level lock of that class
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 407
    
    1

No ! :-)

A class level object just locks the associated class object shared effectively (by association) between the instances, each object has its own lock. Locking on the class object is still just locking on an object is not really that magical when you get your head round it.


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Anirudh Bhatnagr
Greenhorn

Joined: Sep 01, 2010
Posts: 12
So if the class level lock is just another lock on the class object.
Then apart from providing a locking mechanism for static methods what else can be achieved?
Andrey Kozhanov
Ranch Hand

Joined: Mar 12, 2010
Posts: 79
And what do you want to achive? Let's review the following code

The output will be:

m1 is started
m2 is working
m1 is completed


If you want the output to be

m1 is started
m1 is completed
m2 is working


i.e. m2 method is started only after m1 is finished, then you should synchronize m2 code on the same object as m1, that is Test.class object, like this:
Snehal Masne
Greenhorn

Joined: Nov 02, 2013
Posts: 7
Object level locking:

Object level locking is mechanism when you want to synchronize a non-static method or non-static code block such that only one thread will be able to execute the code block on given instance of the class. This should always be done to make instance level data thread safe. This can be done as below :



Class level locking:

Class level locking prevents multiple threads to enter in synchronized block in any of all available instances on runtime. This means if in runtime there are 100 instances of DemoClass, then only one thread will be able to execute demoMethod() in any one of instance at a time, and all other instances will be locked for other threads. This should always be done to make static data thread safe.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class level lock versus Object level lock
 
Similar Threads
synchronized static method
Bodgitt and Scarper : locking and unlocking
static synchronized
Synchronized with example
Synchronized block vs synchronized method