About Volatile
We know that in Java, set a variable value, in addition to the line and double types of variables are atomic operations, that is, the simple read and write operations for variable values are not necessary to synchronize. Before JVM 1.2, Java's memory model implementation always reads variables from the main memory, which is not required for special attention. With the maturity and optimization of JVM, the use of Volatile keywords is now very important in multithreaded environment. Under the current Java memory model, threads can save variables in local memory (such as machine registers), rather than reading and writing directly in the main memory. This may cause a thread to modify a variable value in the main memory, while another thread also continues to use the copy of the variable value in the register, resulting in inconsistent data. To solve this problem, you only need to declare this variable as Volatile, which indicates JVM, this variable is unstable, and this variable is unstable. Read. Generally speaking, the flags of each task between the multi-task environments should be added to Volatile modification.