Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is synchronized block better than synchronized method?

I have started learning synchronization in threading.

Synchronized method:

public class Counter {     private static int count = 0;     public static synchronized int getCount() {       return count;    }     public synchronized setCount(int count) {       this.count = count;    }  } 

Synchronized block:

public class Singleton {     private static volatile Singleton _instance;     public static Singleton getInstance() {       if (_instance == null) {          synchronized(Singleton.class) {             if (_instance == null)                _instance = new Singleton();          }       }       return _instance;    } } 

When should I use synchronized method and synchronized block?

Why is synchronized block better than synchronized method ?

like image 479
Sabapathy Avatar asked Jan 03 '14 15:01

Sabapathy


People also ask

Which is more preferred synchronized block or synchronized method?

Synchronized block is more preferred way because it doesn't lock the Object, synchronized methods lock the Object and if there are multiple synchronization blocks in the class, even though they are not related, it will stop them from execution and put them in wait state to get the lock on Object.

Why might you use a synchronized block?

A Synchronized block is a piece of code that can be used to perform synchronization on any specific resource of the method. A Synchronized block is used to lock an object for any shared resource and the scope of a synchronized block is smaller than the synchronized method.

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.

Is it better to make whole getInstance () method synchronized or just critical section is enough which one you will prefer?

Which one you will prefer? Answer : This is again related to double checked locking pattern, well synchronization is costly and when you apply this on whole method than call to getInstance() will be synchronized and contented.


2 Answers

It's not a matter of better, just different.

When you synchronize a method, you are effectively synchronizing to the object itself. In the case of a static method, you're synchronizing to the class of the object. So the following two pieces of code execute the same way:

public synchronized int getCount() {     // ... } 

This is just like you wrote this.

public int getCount() {     synchronized (this) {         // ...     } } 

If you want to control synchronization to a specific object, or you only want part of a method to be synchronized to the object, then specify a synchronized block. If you use the synchronized keyword on the method declaration, it will synchronize the whole method to the object or class.

like image 161
Erick Robertson Avatar answered Oct 18 '22 05:10

Erick Robertson


Although not usually a concern, from a security perspective, it is better to use synchronized on a private object, rather than putting it on a method.

Putting it on the method means you are using the lock of the object itself to provide thread safety. With this kind of mechanism, it is possible for a malicious user of your code to also obtain the lock on your object, and hold it forever, effectively blocking other threads. A non-malicious user can effectively do the same thing inadvertently.

If you use the lock of a private data member, you can prevent this, since it is impossible for a malicious user to obtain the lock on your private object.

private final Object lockObject = new Object();  public void getCount() {     synchronized( lockObject ) {         ...     } } 

This technique is mentioned in Bloch's Effective Java (2nd Ed), Item #70

like image 31
wolfcastle Avatar answered Oct 18 '22 05:10

wolfcastle