Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of java.lang.Class and how it relates to static method synchronization?

I am reading static method synchronization in java. Where i read static methods get a lock on object of java.lang.class. I was trying to understand the concept of java.lang.class and its role in static method synchronization and i have these questions.

  1. I was reading the blog where it says every class in Java has an instance of java.lang.Class and all instances of a class share this object. Instance of java.lang.Class describes type of object? What is the role of java.lang.Class here? How does it describes type of object?

  2. Secondly for static method synchronization, we need to get the monitor of java.lang.Class. Why is that? Why do we need a lock on java.lang.Class monitor? Why not on the instance of our own class for example Test(my own custom class)?

Can someone elaborate on it. I am really sorry because it sounds a pretty basic question but i am pretty new to this concept.

like image 883
benz Avatar asked Aug 08 '13 10:08

benz


2 Answers

Tentative explanation, although admittedly it is not fully correct. For whatever class C, when you do:

final C c = new C();

two Objects are involved here: the Class<C> object (which is provided via the context classloader) and the c instance. c will know which class it is via its .getClass() method (defined in Object).

The fact that the new keyword is able to establish a "backlink" to the correct Class is the responsibility of the JVM implementation. While this is certainly mentioned in the JLS, I cannot tell where...

Now, more to the point.

If you have a method declared as:

synchronized void whatever() { someCode(); }

then it is roughly equivalent to (why roughly: see below):

void whatever()
{
    synchronized(this) {
        someCode();
    }
}

That is, this code is synchronized at the instance level.

If the method is static however, this:

public static synchronized void meh() { someOtherCode(); }

is roughly equivalent to (why roughly: see below):

public static void meh()
{
    synchronized(getClass()) {
        someOtherCode();
    }
}

One thing to note is that all Class objects are singletons; no matter how many instances of class C you create, .getClass() will always return the same Class object. Try this:

public static void main(final String... args)
{
    final String s1 = "foo";
    final String s2 = "bar";
    System.out.println(s1.getClass() == s2.getClass()); // true
}

Add the fact that getClass() is equivalent to this.getClass() and you get the picture. Class itself being an Object, it obeys the monitor rules of any Object.

And since here we always refer to the exact same object, monitor rules apply ;)

Now, "roughly": in the code written above, the logic is the same; however, depending on how you write that code, the bytecode may differ; but the JIT will have its say in it and will eventually optimize code paths.

like image 119
fge Avatar answered Sep 23 '22 23:09

fge


Every object in java is an instance of some class. In addition to that every class is an object too, so it is an instance of some class too.

Instance of java.lang.Class describes type of object?

Not exactly. java.lang.Class is a class of class instance.

What is the role of java.lang.Class here? How does it describes type of object?

It describes type of all types.

Secondly for static method synchronization, we need to get the monitor of java.lang.Class. Why is that? Why do we need its instances lock not our class lock?

You need to synchronize on some object. Static methods have no access to this, by definition, so the only shared thing that is left is a class where they are defined.

like image 38
Eugene Loy Avatar answered Sep 22 '22 23:09

Eugene Loy