analogy with the main memory and CACHE,JVM of modern computers The all variables are stored in the master RAM (analog computer Each thread then has its own working memory (analogous to the cache per processor). The thread's working memory holds the copy value of the variable that the thread needs to use, and the thread runs on the CPU to read and write to the data in its own worker thread, and the data is synchronized to the main memory after the run is finished. So analogy to the computer using cache consistency protocol to solve the cache consistency problem, the JVM needs a thread synchronization mechanism to achieve multithreading to the same memory region read and write control. in addition, in order to improve performance, the Java compiler took the order reordering (analogy to the computer's disorderly execution), if multiple threads have statements to operate on the same memory area, It is possible that the result is not predictable because of the order reordering. Therefore, the thread synchronization mechanism is also required to achieve multithreading to read and write control of the same memory area. Three: Data interaction between main memory and working memory JVM defines 8 operations to complete the data interaction between main memory and thread working memory: 1:lock: Identifies the main memory variable as a thread exclusive and does not allow other threads to read and write to this variable at this time. 2:unlock: Unlocks a main memory variable. 3:read: Reads a main memory variable value into the working memory of the thread, emphasizing that it is read into the process. 4:load: Saves the read-to-variable value to the thread's working memory as a copy of the variable, emphasizing the process of saving the value that is reading in. 5:use: During thread execution, the value of the variable in the working memory is passed to the bytecode execution engine. 6:assign (Assignment): The bytecode execution engine passes the result of the operation back to the working memory and assigns it to the result variable in the working memory. 7:store: The workThe value of the variable in memory is transferred to the main memory, emphasizing the process of transmission. 8:write: Writes the value of the store's incoming variable into the main memory variable, emphasizing the process of saving. JVM requires that the above 8 operations have atomicity, that is, the data read and write operations are atomic. However, there are exceptions, namely: long, double non-atomic agreement: The two 64-bit types of data read, write operations are required two times, one read/write 32 bits, two reads/Two writes are not guaranteed atomicity. Four: atomicity, visibility, ordering atomicity: Read and write operations of basic data types are atomic The more wide-ranging (code block) atomicity can be implemented with the lock, unlock operation (only one thread executes after locking, so the atomic operation is not interrupted by other threads), and the code level is the use of the syncrhoized synchronization block. Visibility: When a thread modifies the value of a main memory variable shared by multiple threads, the other line Cheng Nen immediately know the change. As we can see above, the JVM is changing the value of the variable in the working memory, synchronizing the new value to the main memory, and then the other thread reads the new value from the main memory to achieve visibility. Here's the difference: when the value of a normal variable changes, it does not necessarily synchronize the main memory immediately, but it will wait until the thread finishes or a period of time to synchronize, and after synchronizing back to main memory, the other thread's working memory does not necessarily read the new value immediately. Variables modified by the volatile keyword, once modified in working memory, are immediately synchronized back to main memory, and the working memory of the other thread that uses the variable immediately reads the new value from main memory. The syncrhoized keyword-modified variable can only be used by one thread at a time, so there is only one worker thread reading and writing it at a time, so it is also possible to "portrait" to achieve visibility. Order: The ordering of the operation of shared data between multiple threads can be ensured by volatile and syncrhoized keywords。 The volatile keyword prohibits command reordering, whereas the Syncrhoized keyword specifies that multiple threads can only have one thread to operate on the shared data at a time. Five: Volatile keyword a volatile variable has two characteristics: visibility, prohibit order reordering. However, volatile does not have atomic nature! The reason is that the value of the volatile variable can be alternately modified by multithreading, and the modifications include read, load, use, store, write, and so on, which are not guaranteed to be atomic. Visibility: Variables modified by the volatile keyword, once modified in working memory, are immediately synchronized back to main memory, and the working memory of other threads that use the variable immediately reads the new value from main memory. prohibit command reordering: Volatile variables create a memory barrier after they are assigned: when the instruction is reordered, the following instruction cannot be queued before the memory barrier. 1, the lock provides two main features: Mutual exclusion (mutual exclusion) and visibility (visibility). Mutual exclusion allows only one thread to hold a particular lock at a time, so you can use this attribute to implement a coordinated access protocol to shared data so that only one thread can use that shared data at a time. Visibility is more complex, and it must ensure that the changes made to the shared data before the lock is released are visible to the other thread that subsequently acquired the lock--if there is no such visibility guarantee provided by the synchronization mechanism, the thread sees the share Variables may be pre-modified or inconsistent values, which can cause many serious problems. (race condition) 2, in Java, in order to ensure data consistency when multithreading read and write data, can be used in two ways: synchronization: With synchronized keyword, or use lock object Use the volatile keyword: to summarize volatile in a nutshell, it enables the variable to be known to other threads as soon as the value changes. 3, volatile First we must first realize that there is such a phenomenon, the compiler in order to speed up the speed of the program, the write operation of some variables will first be in the register or CPU cache, the last to write memory . and in this process, The new value of the variable is not visible to other threads.  When a volatile variable is modified, the variables stored in the other cache are purged and re-read. It's not clear where I'm going to read it. In general, it should be changed to the new value in the modified cache a, then notify the other cache to clear this variable, when the other cache B thread reads this variable, the store will send a message to the bus, the new value of the cache a gets the message, the new value to B. The new value is finally written to memory. This step occurs when a variable needs to be updated, and the action of volatile is the variable that is modified by it, and each time it is updated, the above steps are refreshed. 4, volatile and synchronized1) volatile nature is telling the JVM that the value of the current variable in the register is indeterminate and needs to be read from main memory, synchronized is locking the current variable, and only if the thread can access the variable, Other threads are blocked. 2) volatile can only be used at the variable level, synchronized can be used in variables, methods. 3) volatile can only implement variable visibility, while synchronized can guarantee the change visibility and atomicity of variables. The Java programming idea says that when you define a long or double variable, if you use the volatile keyword, you get (simple assignment and return operation) Atomicity 4) The volatile does not cause a thread to block, and synchronized can cause a thread to block. 5 , when a field's value depends on its previous value, volatile does not work, such as n=n+1,n++. If the value of a domain is limited by the values of other domains, then volatile does not work, such as the lower and upper boundaries of the range class, and must follow the lower<=upper limit. 6. The only security scenario that uses volatile instead of synchronized is that there is only one mutable domain in the class.
Reprint learning Memory models and keywords in multi-threading