Home > DeveloperSection > Interviews > What is the use of voltile variable in java

Posted on    September-04-2015 6:53 AM

 Java Java 
 1 Answer(s)
  606  View(s)
Rate this:

Mayank Tripathi
Mayank Tripathi

Total Post:807

Posted on    September-04-2015 6:53 AM

The Java volatile keyword is used to mark a Java variable as "being stored in main memory". More precisely that means, that every read of a volatile variable will be read from the computer's main memory, and not from the CPU cache, and that every write to a volatile variable will be written to main memory, and not just to the CPU cache.

The Java volatile keyword guarantees visibility of changes to variables across threads. In a multithreaded application where the threads operate on non-volatile variables, each thread may copy variables from main memory into a CPU cache while working on them, for performance reasons. If your computer contains more than one CPU, each thread may run on a different CPU. That means, that each thread may copy the variables into the CPU cache of different CPUs.

Don't want to miss updates? Please click the below button!

Follow MindStick