This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class level lock versus Object level lock

 
Anirudh Bhatnagr
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So does a class level lock stop a thread to acquire Object level lock of that class
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Anirudh Bhatnagr
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic