3

If I have a method like this

public synchronized static foo()
{

}

this compiles fine.

So this leads me to two questions

  1. What is is syncrhonizing on? The Class, or something else?
  2. Can you synchronize on a class, does that lock all object of that class as well.

for instance could you do this

synchronized(Foo) // where Foo is a class
{
}
3
  • Yes, it is synchronizing on the Class object. I'm not sure if I understand your second question correctly, could you please restate it? Commented Apr 16, 2012 at 1:32
  • 1) It is synchronizing on the Class object. 2) It should really be synchronized(Foo.class) Commented Apr 16, 2012 at 1:34
  • So it is synchronizing on Foo.getClass() essentially or the class Class object? Commented Apr 16, 2012 at 1:36

4 Answers 4

3

Synchronized static method in MyClass is essentially the same as synchronized(MyClass.class) block. Your second example should be rewritten as

synchronized(Foo.class) {
}

to be correct.

If you would like to be defensive about your class synchronization, you should synchronize on a private static object not visible outside your class. This prevents malicious code from blocking your static methods by executing synchronized on their class object, thus blocking the legitimate method.

As far as "locking all objects" goes, non-static methods marked synchronized will not be locked by execution of a static synchronized method, because regular synchronized methods lock on an instance of the object, not on its class.

Sign up to request clarification or add additional context in comments.

2 Comments

are they syntactically that same, will it cause a compiler error if I don't use Foo.class, does the compiler know what I mean?
@rubixibuc The compiler expects an expression that evaluates to an object to be supplied inside the parentheses of the synchronized block; since Foo is not an expression that evaluates to an object, you should get a compile error.
3

Yes, it is synchronizing on the Class object. You can synchronize on any Class object. You can either use its literal (Foo.class), or use the getClass() method of an instance of Foo.

For more information about synchronized (the second page in that trail contains the information about which lock synchronized methods use): http://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html

4 Comments

Doe Foo by itself implicitly refer to the class object always?
@rubixibuc No, it does not, it refers to the type Foo, not the Class.
Does is refer in the case of a syncrhonization block?
@rubixibuc The synchronized block takes an object as its argument. Foo is not an object, but Foo.class is.
2
  1. It is synchronizing on the class object, eg MyClass.class
  2. No, it doesn't lock objects of the class

Comments

0

Static methods use the class as the object for locking.

The relevant section on the Java Language Specification is 8.4.3.6, 'synchronized Methods':

A synchronized method acquires a monitor (§17.1) before it executes. For a class (static) method, the monitor associated with the Class object for the method's class is used. For an instance method, the monitor associated with this (the object for which the method was invoked) is used.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.