This tutorical covers about the differences between synchronized and volatile modifier . To know lot more about volatile keyword and how it works in java? , please visit my earlier tutorial volatile modifier in java . The following are the differences between synchronized and volatile modifier which is one of the core java interview question..
1. Synchronization ensures both visibilty and mutual exclusion whereas volatile ensures only visibilty ( one thread reads the most up-to-date value written by another thread)
2. The volatile modifier is applied to a field variable whereas synchronized is applied to methods and block of codes.
3. When using synchronized modifier, an unlock on monitor happens before / synchronizes with all subsequent lock on that monitor.When using volatile , a write to a volatile variable happens before with all subsequent reads of that variable by any thread
4. Volatile does not guarantee the atomicity of composite operations such as incrementing a variable where as synchronized modifier guarantees the atomicity of composite operations.
for example
private static volatile int sno = 0;
public static int getNextSno() {
return sno++;
}
The above method won’t work properly without synchronization. Every invocation may not return unique value because increment operator performs three operations read, add, store. first it reads the value then increments plus one then it stores back a new value. When a thread reads the field , another thread may enter and read the same value before the first thread writes the incremented value back . Now both thread returns the same sno. Use synchronized on the method getNextSno(), to ensure that the increment operation is atomic and remove volatile modifier from sno.
4. A thread acquires or releases a lock on a monitor to enter or exit a synchronized method or block of code where as a thread does not need to aquire / release a monitor to read or write a volatile variable. i.e. but both have the same memory effects.
5. final variable can not be a volatile where as final method can be synchronized
6. volatile variable can be null where as synchronization can not be done on null object .
7. In some situations , code is simplified using volatile than using synchronized. At the same time, we have to be careful when using volatile , code using volatile is weaker (easily broken) than synchronized as shown in the above example (4th point).
1. Synchronization ensures both visibilty and mutual exclusion whereas volatile ensures only visibilty ( one thread reads the most up-to-date value written by another thread)
2. The volatile modifier is applied to a field variable whereas synchronized is applied to methods and block of codes.
3. When using synchronized modifier, an unlock on monitor happens before / synchronizes with all subsequent lock on that monitor.When using volatile , a write to a volatile variable happens before with all subsequent reads of that variable by any thread
4. Volatile does not guarantee the atomicity of composite operations such as incrementing a variable where as synchronized modifier guarantees the atomicity of composite operations.
for example
private static volatile int sno = 0;
public static int getNextSno() {
return sno++;
}
The above method won’t work properly without synchronization. Every invocation may not return unique value because increment operator performs three operations read, add, store. first it reads the value then increments plus one then it stores back a new value. When a thread reads the field , another thread may enter and read the same value before the first thread writes the incremented value back . Now both thread returns the same sno. Use synchronized on the method getNextSno(), to ensure that the increment operation is atomic and remove volatile modifier from sno.
4. A thread acquires or releases a lock on a monitor to enter or exit a synchronized method or block of code where as a thread does not need to aquire / release a monitor to read or write a volatile variable. i.e. but both have the same memory effects.
5. final variable can not be a volatile where as final method can be synchronized
6. volatile variable can be null where as synchronization can not be done on null object .
7. In some situations , code is simplified using volatile than using synchronized. At the same time, we have to be careful when using volatile , code using volatile is weaker (easily broken) than synchronized as shown in the above example (4th point).
No comments:
Post a Comment