This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes class level in synchronized blocks 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 in synchronized blocks" Watch "class level in synchronized blocks" New topic
Author

class level in synchronized blocks

Bhagya Nayanala
Greenhorn

Joined: Oct 12, 2012
Posts: 3
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

Joined: Mar 10, 2008
Posts: 1765
    
    7

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.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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 Nayanala
Greenhorn

Joined: Oct 12, 2012
Posts: 3
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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 Nayanala
Greenhorn

Joined: Oct 12, 2012
Posts: 3
Thanks a lot that clears my doubt
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Aug 10, 2009
Posts: 4
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

Joined: Aug 10, 2009
Posts: 4
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class level in synchronized blocks