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 ?
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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With