Original works, can be reproduced, but please mark the source address: http://www.cnblogs.com/V1haoge/p/7833881.html
Volatile is said to be the most lightweight concurrency control method in the Java language.
Volatile can guarantee the visibility of variables, what does it mean?
visibility means that after a thread has modified the variable, other threads can immediately discover and use the modification (a thread's modifications are visible to other threads).
How visibility is implemented: volatile controls the way in which main memory and working memory interact in the Java memory model. Volatile ensures that a thread's changes to its modified variables are immediately written to the main memory, while ensuring that each read to its modified variable is fetched directly from main memory (that is, volatile enforces binding of the assign assignment to the store and write operations. The use Action Force is bound to the read, load operation so that the store, write operation must be performed after the assign, and the using operation must first execute read, Load operation) This ensures that the values of the variables read by other threads are up-to-date (please understand the Java memory model first if you are unfamiliar with these operations).
Note: Volatile can achieve visibility, but atomic operations cannot be implemented. Atomic operations are associated with Java code and are not a keyword that can be controlled. (please distinguish between visibility and atomic operations, which I was confused with before, and immediately transparent after separation)
There are many ways to implement atomic operations in Java, but they do not contain volatile and are implemented in a simple way: atomic operations under the atomic package (implemented through CAs), read and write operations for basic data types, and so on.
Java Classic non-atomic operations such as self-increment implementation, the ordinary i++ operation seems to have only a sentence, but compiled into a machine instruction after the number of rows, not known, the pit top is not a sentence can be achieved, so many commands to execute of course can not guarantee the atomic nature, At this time we can atomicinteger and Atomiclong atomic Operation Class Getandincrement () method to implement.
Another function of volatile is to avoid reordering and to use memory barriers to prevent reordering. In a single-threaded environment, of course there is no need to prohibit reordering, but the code executed after reordering in a multithreaded environment can have errors, such as the need to detect the value of a variable in thread B in thread A, and if the variable is not decorated with volatile, the operation on that variable in thread B may be reordered. may be executed in advance, which is once the action is executed, then thread A can get the value of this variable in advance (perhaps in the case that some thread A's preparation is not yet fully prepared, assuming that these preparations are defined in threads B, but there is no dependency on the variable operation, and once the variable operation is advanced, these preparations lag , which is where thread A will start executing the following code when the preparation is not complete, causing an error. When you add a volatile modifier to a variable, it disables the reordering of its operations, guaranteeing that the operation is performed after all the preparation is done, and then thread a starts up in a ready state to perform normally.
Simple understanding of volatile in Java