Ignored twin brother of synchronised, The volatile
I guess volatile is the most ignored keyword among normal java folks, everyone knows there is a volatile keyword but no one know why. So I will explain the purpose for volatile keyword.
volatile keyword is to indicate java and other threads not to cache the local variable value but read/write it from main memory every time.
volatile keyword is to variables what synchronised is to code blocks.
volatile keyword is to ensure atomicity of local variables in a multi threaded environment, where different threads do the reading and writing on same local variable.
Atomicity
Let take the example of Double checking Singleton implementation from previous post.
If we do not make INSTANCE variable volatile then Thread which is creating instance of Singleton will not able to communicate with other threads that an instance has been created, until it comes out of the synchronised block, so if Thread A is creating Singleton instance and just after creation lost the CPU, all other thread will not be able to see value of INSTANCE as not null and they will believe its still null.
Reader threads are not doing any locking and until writer thread comes out of synchronized block, memory will not be synchronized and value of INSTANCE will not be updated in main memory. With Volatile keyword such updates will be visible by all reader threads.
volatile for long and double
One more instance where volatile is useful is when writing to long and double variables in multithreaded environment.long and double are 64bit and many platforms(32 bit) perform writing to these variables in 2 steps, and to attain atomicity to writing of these variables, we should make these variables volatile.
One more important aspect of volatile is it ensures ordering of the statement execution,
Ordering
Java is taught as Jit and executes line by line as written by us, it is not completely true for modern compiles and runtimes, modern compilers and runtimes do a lot of re-ordring of your code in order to optimise performance, and as any program these optimisers aren't perfects, and may lead to un desirable effects in highly multi threaded environments.
Volatile keyword here helps to inform java to not mess around with my code, and i need it as i write it.
Examples
We may not encounter many scenarios where we need to use volatile keyword unless we are into game programming or developing servers, few examples which i find interesting are.
Lets say i am writing a game related to fighting, and i maintain a shared boolean variable isDead, now when a player is dead one thread(yama) updates the variable to true and other threads will act as per it state, say the rendering engine starts making player to fall down, spill blood etc.
Now lets say, if isDead is not volatile and the thread which updated the variable did not get cpu after updating variable to true or is performing some time consuming tasks like updating servers etc., before completion of its code block. Well you can imagine the rest.













