For the first time, I came into contact with keyword volatile, I don't know why, just blurry remember that the Java keyword seems to have it. I checked some information, organized as follows:
Volatile modified member variables are forced to reread the value of the member variable from shared memory every time you are accessed. Moreover, when the member variable changes, force threads will write the change value back to the shared memory. This is at any time, two different thread always see the same value of a member variable.
The Java language specification points out: in order to obtain the best speed, the thread is allowed to save the private copy of the shared member variable, and only the original value of the shared member variable only when the thread enters or leaves the synchronous code block.
This must be noted when multiple threads interact with an object simultaneously, you must pay attention to changes in the shared member variables to make threads.
The volatile keyword is a prompt VM: The private copy cannot be saved for this member variable, but should be interactively interact with the shared member variable.
Use suggestions: Use Volatile on the member variables accessed by two or more threads. When the variable to be accessed is already in the synchronized code block, it is not necessary when it is constant.
Since the VOLATILE shields are used to remove the necessary code optimization in the VM, it is relatively low in efficiency, so this keyword must be used if necessary.