• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

class level in synchronized blocks

 
Bhagya Shreee
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i have a doubt

synchronized(Classname.Class)
{
/*
*/
}

The lock obtained in the above code is this exactly same as the lock obtained by executing a static synchronized method?

if there is any difference please explain
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, and welcome to JavaRanch!
A static synchronized method declaration uses the intrinsic lock of that particular type's Class object.
If you use a synchronized statement within a static method you must explicitly provide a reference to an object whose intrinsic lock will be used.
This does not necessarily have to be a reference to the Class object of that type (the .class syntax), but it may be and in that case all static synchronized methods will indeed synchonize on the same lock.
Just never, ever specify a reference to an instance level object for a synchronized statement in a static context.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhagya Nayanala wrote:hi,
i have a doubt

synchronized(Classname.Class)
{
/*
*/
}

The lock obtained in the above code is this exactly same as the lock obtained by executing a static synchronized method?


If the above code is in a method in a class called "Classname" and the sync block encloses the entire method body, then yes, it's equivalent. Declaring a method synchronized is just syntactic sugar for synchronizing the whole method body on the Class object (static methods) or on "this" (non-static methods).
 
Bhagya Shreee
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im getting confused

in the first case

class X
{
public synchronized m1()
{
}

public static synchronized m2()
{
}

}


here when a thread executes m2() it will acquire a class level lock and when another thread executes m1() method then it will acquire an object level lock and these two methods can run simultaneously.

now my doubt is

when i take a synchronized block this way

synchronized(X.class)
{


}

now this one will acquire a class level lock. So will it effect executing any instance methods from any thread?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhagya Nayanala wrote:Im getting confused





here when a thread executes m2() it will acquire a class level lock and when another thread executes m1() method then it will acquire an object level lock


There's no such thing as a "class level lock" or "object level lock". They're all just locks and they're all identical. Every object has exactly one lock associated with it, including java.lang.Class objects. Synchronizing on an object just obtains that object's lock and prevents other threads from obtaining it. It doesn't matter if the lock belongs to a java.lang.Class object or to some other object. They all behave identically.


when i take a synchronized block this way

synchronized(X.class)
{


}

now this one will acquire a class level lock. So will it effect executing any instance methods from any thread?


It will acquire a lock on a particular object, exactly like any other synchronized block or method does. And just like any other synchronized block or method, all it does in terms of mutual exclusion is prevent other threads from acquiring that same lock.

There's no such thing as a "class level lock", and the lock of a java.lang.Class object is no different from any other object's lock.
 
Bhagya Shreee
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot that clears my doubt
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
There's no such thing as a "class level lock", and the lock of a java.lang.Class object is no different from any other object's lock.

in fact, there is different . two different threads on two different object can have no restriction to enter two different synchronized methods simultaneously unless one of them is static .
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:
Jeff Verdegan wrote:
There's no such thing as a "class level lock", and the lock of a java.lang.Class object is no different from any other object's lock.

in fact, there is different . two different threads on two different object can have no restriction to enter two different synchronized methods simultaneously unless one of them is static .


No, there's no difference. What you describe is not a difference in the locks. It's only a difference in which object's lock the syntactic sugar gives us.

In all cases, synchronization amounts to synchronized(X), where X is some object, and all that does is to prevent other threads from obtaining X's lock. Doesn't matter if X is an instance of java.lang.Class or some other class.
 
Mukund Desai
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhagya Nayanala wrote:hi,
i have a doubt

synchronized(Classname.Class)
{
/*
*/
}

The lock obtained in the above code is this exactly same as the lock obtained by executing a static synchronized method?

if there is any difference please explain


Yes, the lock obtained will be on the same object.
 
Mukund Desai
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhagya Nayanala wrote:Im getting confused

in the first case

class X
{
public synchronized m1()
{
}

public static synchronized m2()
{
}

}


here when a thread executes m2() it will acquire a class level lock and when another thread executes m1() method then it will acquire an object level lock and these two methods can run simultaneously.

now my doubt is

when i take a synchronized block this way

synchronized(X.class)
{


}

now this one will acquire a class level lock. So will it effect executing any instance methods from any thread?


No, it won't affect instance methods execution.
But it will surely affect all static menods,
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mukund Desai wrote:
Yes, the lock obtained will be on the same object.


Hi, and welcome to the Ranch!

This answer was already given a few weeks ago. Please do read the thread before adding additional comments.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mukund Desai wrote:
But it will surely affect all static menods,


No.

It will only affect other methods or blocks of code syncing on the same lock. It won't affect any static methods that are not synchronized.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic