Volatile Keyword in Java

  • You can use the volatile keyword with variables. Using volatile keyword with classes and methods is illegal.
  • It guarantees that value of the volatile variable will always be read from the main memory, not from the local thread cache.
  • If you declared variable as volatile, Read and Writes are atomic
  • It reduces the risk of memory consistency error.
  • Any write to volatile variable in Java establishes a happen before the relationship with successive reads of that same variable.
  • The volatile variables are always visible to other threads.
  • The volatile variable that is an object reference may be null.
  • When a variable is not shared between multiple threads, you do not need to use the volatile keyword with that variable.