32

Possible Duplicate:
Java Synchronized Block for .class

I was reading through an article on synchronization. I am confused on below points and need more clarification

  1. For synchronization block. How

    synchronized (this) {
        // code
    }
    

    differs from

    synchronized (MyClass.class) {
        // code
    }
    
  2. Synchronizing instance method means threads will have to get exclusive lock on the instance, while synchronizing static method means thread will have to acquire a lock on whole class(correct me if I am wrong). So if a class has three methods and one of them is static synchronized then if a thread acquires lock on that method then that means it will acquire lock on the whole class. So does that mean the other two will also get locked and no other method will be able to access those two methods as the whole class is having lock?

user3840170
  • 26,597
  • 4
  • 30
  • 62
Sandeep Kumar
  • 13,799
  • 21
  • 74
  • 110

2 Answers2

36

MyClass.class and this are different things, they are different references to different objects.

this - is a reference to this particular instance of the class, and

MyClass.class - is a reference to the MyClass description object.

These synchronization blocks differ in that the first will synchronize all threads that deal concretely with this instance of MyClass, and the second one will synchronize all threads independently of which object on which method was called.

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
13

The first example (acquiring lock on this) is meant to be used in instance methods, the second one (acquiring lock on class object) -- in static methods.

If one thread acquires lock on MyClass.class, other threads will have to wait to enter the synchronized block of a static method that this block is located in. Meanwhile, all of the threads will be able to acquire lock for a particular instance of this class and execute instance methods.

Andrew Logvinov
  • 21,181
  • 6
  • 52
  • 54
  • only static class vars will be locked with synchronize(MyClass.class) right? – tgkprog May 22 '13 at 21:29
  • @tgkprog You don't synchronize variables themselves, you synchronize access to them (via methods). `MyClass.class` is just an object and it is possible (though I can't imagine real case when it would be needed) to synchronize instance methods on it too, but in general this paradigm is used with static methods. – Andrew Logvinov May 23 '13 at 06:57
  • hmm i thought synchronize(MyClass.class) locks up only static variables declared in the class. will have to look it up. So your saying if i had 1,000 instances of my class, each with 100 variables, they would all be locked? – tgkprog May 23 '13 at 08:52
  • 1
    `MyClass.class` is just an object, just like `this`. Answering your question, yes, if all of the methods acessing these variables are synchronized on the same object. – Andrew Logvinov May 23 '13 at 10:13
  • i did some rading, your right. you would use it with static vars but no reason why it can't be used for a global lock n non static block/ vars – tgkprog May 23 '13 at 22:06