The JVM is divided into JVM client mode, JVM server mode. The difference is not big, but the serve startup mode starts slowly, but once it is running, the efficiency in hash and method call is dozens of times times higher than the client mode.
When the thread reads the data, it first reads the data from the main memory, saves it in the local working memory, modifies the data, modifies it, writes to the local working memory, and then writes the main memory from the working memory. The example diagram is as follows:
If the JVM is started in server mode, for the sake of thread efficiency, the thread will save the data in working memory after the first time it is read, and then read the data from the working memory. However, if the main memory modifies the data, it causes the data in the working memory to be inconsistent with the main memory data. Causes a logic error.
The volatile keyword can only modify variables. When a variable is modified with volatile, each read variable is forced to read from the main memory, which guarantees the consistency of the variables. Data is guaranteed to be visible, but it does not guarantee the atomicity of the data.
Java multithreaded volatile keyword