What is volatitle variable?
volatile variable in Java is a special variable which is used to signal threads, a compiler that this particular variables value are going to updated by multiple threads.
- Variable’s value is always read from main memory instead of cached value.
- Volatile variable guarantees “happens-before” relationship, which means not only another thread has visibility of latest value of volatile variable but also all the variable is seen by the thread which has updated value of volatile variable before these thread sees it.