Java™The language includes two internal synchronization mechanisms: Synchronous block (or method) and volatile variables. Both mechanisms are proposed to achieve code thread security. Among them, the synchronization of Volatile variables is poor (but sometimes it is simpler and has lower overhead), and its usage is more error-prone. When talking about the volatile keyword, I have to mention one article: Java Theory and Practice: Correct Use of Volatile variables. This article elaborates on the usage of volatile keywords. The reason why volatile is not commonly used is that this keyword is also very important in high-performance multi-threaded programs, but this keyword may cause many problems. First, let's consider a question: why does variable need volatile to be modified? To clarify this problem, we should first understand what is done inside the computer. For example, after performing an I ++ operation, the computer performs three internal operations: read, modify, and write. Similarly, a value assignment operation is performed for a long data type. In 32 systems, it takes two steps to complete the assignment. First, modify the low 32 bits, and then modify the high 32 bits. Assume that when you place the preceding operations in a multi-threaded environment, some of these steps are executed, and another thread has referenced the variable value, this leads to the problem of reading dirty data. With this assumption, it is not difficult to understand the volatile keyword. Volatile can be used before any variable, but cannot be used before the final variable, because the final variable cannot be modified. There is no thread security issue.