About volatile



In Java volatile keyword is used with member variables to mark a Java variable as being stored in main memory. It means that every read operation on a volatile variable read its value from the main memory, not from the cache of the CPU, and that every write operation on a volatile variable will be write content direct to main memory allocated to the volatile member, and not just to the CPU cache. So, when multiple thread works on a volatile member then get the updated values as a atomic change.
Here is how to declare volatile member.
class SharedData{
    public volatile int counter = 0;
}