synchronized static methods?

K

Knute Johnson

If you have a class with more than one synchronized static method, are
the methods synchronized from each other? What object are they
synchronized on? I know that synchronized instance methods are
synchronized on 'this' but there is no 'this' with static methods/
 
L

Lee Fesperman

Knute said:
If you have a class with more than one synchronized static method, are
the methods synchronized from each other? What object are they
synchronized on? I know that synchronized instance methods are
synchronized on 'this' but there is no 'this' with static methods/

They're synchronized on the associated 'Class' object ... equivalent to:

static void method()
{
synchronized (ClassName.class)
{
...
}
}
 
M

Mike Schilling

Lee Fesperman said:
They're synchronized on the associated 'Class' object ... equivalent to:

static void method()
{
synchronized (ClassName.class)
{
...
}
}

Someone pointed out to me once that there's nothing in the Java docs that
says explicitly "All calls to ClassName.class" will result in the same
object" [1], but the fact that static synchronization works the way it does
implies this.

1. It isn't, as far as I know, guaranteed that all calls to
Class.getMethod(), for instance, result in the same Method object.
 
T

Thomas Hawtin

Mike said:
1. It isn't, as far as I know, guaranteed that all calls to
Class.getMethod(), for instance, result in the same Method object.

I should hope not. java.lang.reflect.Method objects are mutable.

Tom Hawtin
 
L

Lee Fesperman

Mike said:
Lee Fesperman said:
They're synchronized on the associated 'Class' object ... equivalent to:

static void method()
{
synchronized (ClassName.class)
{
...
}
}

Someone pointed out to me once that there's nothing in the Java docs that
says explicitly "All calls to ClassName.class" will result in the same
object" [1], but the fact that static synchronization works the way it does
implies this.

Yep, it seems that the Class object for a given class should be 'unique'. The javadocs
I'm looking at sorta imply that this is true by explicitly saying that Class objects for
each type of array are unique -- "Every array also belongs to a class that is reflected
as a Class object that is *shared* by all arrays with the same element type and number
of dimensions." (emphasis mine). The javadocs for Class.forName() also say "Returns the
Class object associated with the class or interface with the given string name.", rather
than saying "Returns a Class object".

I also think it should be unique within a given classloader but not between
classloaders, lest unexpected (and unfortunate) conflicts arise.

Synchronization of static methods provide another distinction from instance methods.
Synchronized instance methods in super/sub classes all synchronize on the same monitor
within an instance. A synchronized static method synchronizes on a different monitor
than a synchronized static method in its superclass.
1. It isn't, as far as I know, guaranteed that all calls to
Class.getMethod(), for instance, result in the same Method object.

As Thomas mentioned, Method objects are mutable ... because of setAccessible().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top