In which situation can volatile replace synchronized?Also how can the write operations on double and long become atomic after declaring them as volatile
-
s/volatile/synchronized or s/synchronized/volatile will always lead to a compile error... `volatile { ... }`? `public synchronized String s;`? – djechlin Jun 24 '14 at 13:45
-
Both of these are vaguely related to multithreaded code. You should learn what each of them are before asking their "differences". It's like asking the difference between a seatbelt and a paramedic. – djechlin Jun 24 '14 at 13:46
2 Answers
Difference between volatile and synchronized in java
synchronized modifies code blocks and methods where as volatile keyword is used as a modifier with variable.
In Java, volatile keyword only synchronizes the value of one variable between thread memory and main memory while synchronized keyword synchronizes the value of all variable between thread memory and main memory
volatile works faster as compare to synchronized because synchronized affects a lot on performance due to obtain and release of lock.
Obtaining and releasing lock is not required by volatile but it is necessary for synchronized.
Read a very good article for the difference-between-volatile-and- synchronize.
Explaining the same here for clarity -
int i1; int geti1() {return i1;}
volatile int i2; int geti2() {return i2;}
int i3; synchronized int geti3() {return i3;}
geti1() accesses the value currently stored in i1 in the current thread. Threads can have local copies of variables, and the data does not have to be the same as the data held in other threads. In particular, another thread may have updated i1 in it's thread, but the value in the current thread could be different from that updated value. In fact Java has the idea of a "main" memory, and this is the memory that holds the current "correct" value for variables. Threads can have their own copy of data for variables, and the thread copy can be different from the "main" memory. So in fact, it is possible for the "main" memory to have a value of 1 for i1, for thread1 to have a value of 2 for i1 and for thread2 to have a value of 3 for i1 if thread1 and thread2 have both updated i1 but those updated value has not yet been propagated to "main" memory or other threads.
On the other hand, geti2() effectively accesses the value of i2 from "main" memory. A volatile variable is not allowed to have a local copy of a variable that is different from the value currently held in "main" memory. Effectively, a variable declared volatile must have it's data synchronized across all threads, so that whenever you access or update the variable in any thread, all other threads immediately see the same value. Generally volatile variables have a higher access and update overhead than "plain" variables. Generally threads are allowed to have their own copy of data is for better efficiency.
Executing geti3() does the following:
- The thread acquires the lock on the monitor for object this .
- The thread memory flushes all its variables, i.e. it has all of its variables effectively read from "main" memory .
- The code block is executed (in this case setting the return value to the current value of i3, which may have just been reset from "main" memory).
- (Any changes to variables would normally now be written out to "main" memory, but for geti3() we have no changes.)
- The thread releases the lock on the monitor for object this.
So where volatile only synchronizes the value of one variable between thread memory and "main" memory, synchronized synchronizes the value of all variables between thread memory and "main" memory, and locks and releases a monitor to boot. Clearly synchronized is likely to have more overhead than volatile.

- 23,126
- 28
- 107
- 185
None!
Majorly volatile is for variables and synchronized is for methods/blocks.
Volatile keyword is used to ensure visibility across threads in a multi threaded environment. If variable is not volatile it is possible for two different threads to have tow different local copies. making it volatile ensure same value is reflected in all the threads.
synchronized is used for methods/blocks. These methods or block are critical regions whose monitor needs to be locked before accessing it. Each object has an associated monitor. As long as a thread holds a monitor no other threads can access critical regions governed by that monitor.

- 66,731
- 38
- 279
- 289
-
2`volatile` only guarantees visibility (and ordering) - `synchronized` also guarantees atomicity. – assylias Jun 24 '14 at 13:44
-
-