Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

synchronize(this) vs synchronize(MyClass.class) [duplicate]

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?

like image 529
Sandeep Kumar Avatar asked Jan 24 '13 07:01

Sandeep Kumar


People also ask

Is it better to make the whole method synchronized or only critical section synchronized?

To acquire a lock on an object for a specific set of code block, synchronized blocks are the best fit. As a block is sufficient, using a synchronized method will be a waste. More specifically with Synchronized Block , it is possible to define the object reference on which are want to acquire a lock.

What is the advantage of a synchronized block over a mutex?

The main difference is that if you use a synchronized block you may lock on an object other than this which allows to be much more flexible.

Do synchronized methods synchronize on the object or the class?

Synchronized static methods are synchronized on the class object of the class the synchronized static method belongs to. Since only one class object exists in the Java VM per class, only one thread can execute inside a static synchronized method in the same class.

Do not synchronize on objects that may be reused?

Misuse of synchronization primitives is a common source of concurrency issues. Synchronizing on objects that may be reused can result in deadlock and nondeterministic behavior. Consequently, programs must never synchronize on objects that may be reused.


2 Answers

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.

like image 86
Andremoniy Avatar answered Oct 17 '22 08:10

Andremoniy


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.

like image 45
Andrew Logvinov Avatar answered Oct 17 '22 07:10

Andrew Logvinov