Java Synchronized Block for .class

46,760

Solution 1

The snippet synchronized(X.class) uses the class instance as a monitor. As there is only one class instance (the object representing the class metadata at runtime) one thread can be in this block.

With synchronized(this) the block is guarded by the instance. For every instance only one thread may enter the block.

synchronized(X.class) is used to make sure that there is exactly one Thread in the block. synchronized(this) ensures that there is exactly one thread per instance. If this makes the actual code in the block thread-safe depends on the implementation. If mutate only state of the instance synchronized(this) is enough.

Solution 2

To add to the other answers:

static void myMethod() {
  synchronized(MyClass.class) {
    //code
  }
}

is equivalent to

static synchronized void myMethod() {
  //code
}

and

void myMethod() {
  synchronized(this) {
    //code
  }
}

is equivalent to

synchronized void myMethod() {
  //code
}

Solution 3

No, the first will get a lock on the class definition of MyClass, not all instances of it. However, if used in an instance, this will effectively block all other instances, since they share a single class definition.

The second will get a lock on the current instance only.

As to whether this makes your objects thread safe, that is a far more complex question - we'd need to see your code!

Share:
46,760

Related videos on Youtube

Andrew Tobilko
Author by

Andrew Tobilko

Updated on July 08, 2022

Comments

  • Andrew Tobilko
    Andrew Tobilko almost 2 years

    What does this java code mean? Will it gain lock on all objects of MyClass?

    synchronized(MyClass.class) {
       //is all objects of MyClass are thread-safe now ??
    }
    

    And how the above code differs from this one:

    synchronized(this) {
       //is all objects of MyClass are thread-safe now ??
    }
    
  • pstanton
    pstanton over 14 years
    yes, MyClass.class could be any static variable and have the same effect.
  • liwp
    liwp over 14 years
    "as many threads may enter the block as there are instance" implies that the second form acts as a semaphore which is not true. You should say something like: "synchronised(this) ensures that only one thread can enter the block for a given instance of the class".
  • Thomas Jung
    Thomas Jung over 14 years
    Corrected. I intended to say that.
  • kurtzbot
    kurtzbot almost 10 years
    It took me a second reading to catch that the first two examples have the keyword "static". Just pointing that out to others who may have seen this and missed it. Without the static keyword the first two examples would not be the same.
  • Weishi Z
    Weishi Z almost 8 years
    what is the class instance vs the instance?
  • JacksOnF1re
    JacksOnF1re about 6 years
    Those examples are NOT equivalent! The synchronized methods are "synchronized" as a hole when a thread tries to call the methods. The blocks on the other hand, can have code above and below them, that can get executed from multiple threads. They only synchronize within the block! That is not the same!
  • JacksOnF1re
    JacksOnF1re about 6 years
    public static Singleton getInstance() { if (instance == null) { synchronized (Singleton.class) { instance = new Singleton(); } } return instance; }
  • Jorn
    Jorn about 6 years
    The whole point is that there is no code outside the synchronized blocks. That makes them equivalent. If you change one example, they are indeed no longer the same.
  • krupal.agile
    krupal.agile almost 5 years
    So, if you have a static method and we don't want to synchronize all of its body, then we synchronized(this) is not good, instead synchronized(Foo.class) is appropriate. Is that right?
  • tomorrow
    tomorrow over 2 years
    Just adding that synchronized(MyClass.class) can also be used from other classes, not just from withing MyClass. I have a legacy code where several classes use a method from one class (let's say Foo.saveStuff). I need to make sure that only one thread uses saveStuff at a time. Due to bad DB transaction design I can't just make safeStuff synchronized so I have to use synchronized(Foo.class) in all the other methods.
  • Jorn
    Jorn over 2 years
    @tomorrow In that case, it's probably better to synchronize on a separate lock object, not on some Class instance.
  • Enerccio
    Enerccio over 2 years
    this is not true, yes it will block all instances that are from same classloader!
  • Enerccio
    Enerccio over 2 years
    synchronized(X.class) is used to make sure that there is exactly one Thread in the block. this is false, it depends on how many classloaders you have