The Volatile keyword
Let’s start with a concept: JMM memory model diagram:
Each thread at the time of operation will have a belongs to own the working memory (i.e., the local memory) in the above, the variable of the existence of working memory is from the main physical memory (i.e., the above main memory), so at the time of change, need to work in the local memory first, then wrote the main physical memory, Other threads again to get the value of physical memory, ensure the data in each thread is visible, this time may be some doubt why don’t you directly in the main physical memory operation, the reason is that the main physical memory doesn’t direct operation, otherwise it wouldn’t have existed in local work, it is important to note the JMM model is not real, but rather a set of specifications.
The body of the
Volatile guarantees visibility and order. It is important to note that volatile does not guarantee atomicity and prohibits instruction reordering. It is commonly used in concurrent programming. Indivisible and either successful or failed at the same time, atomic classes such as AtomicInteger can be used to solve the problem that volatile is not atomic. What instruction rearrangement, Java code is compiled in the bottom, will be carried out in order rearrangement, the underlying computer code in the implementation of the sometimes will not walk in you write the code order, in order to performance it may reorder, but all have to follow the principle of data dependencies (that is, to reorder must first ensure that defines this thing), details see below:
What happens in the concrete code example:
CAS
The underlying class, Unsafe, is the CAS concurrency primitive, And its expression in Java is the various methods in the Unsafe class. Broadening broadening concerns over food, for instance, is an Unsafe primitive that checks whether a value in memory is changed or not. Because the CAS system is a system primitives, belong to the category of language system, is composed of a number of command, is used to accomplish a function of a process, and carry out must be continuous, primitive is not permitted to interrupt in the implementation process, that is to say the CAS is a CPU atomic instruction, will not result in a so-called data inconsistency problem. Unsafe all methods are native, meaning they directly manipulate underlying operating system resources to perform responsive tasks
Application: There are three values in CAS, memory value V, old expected value A, and value B to be changed if and only if A is equal to V, change V to B, otherwise nothing can be done using the Atomic class, as shown in the figure below:
The CAS method is actually a spin lock. When the condition is true, it breaks out of the loop. When the condition is not true, it keeps spinning, as shown in the figure:
No one is perfect, everything in the world is the same truth, even if CAS is so good, he also has a disadvantage, is to think, if you can not jump out of the cycle, what will happen? Right! It puts a lot of pressure on the CPU
So here, we have to mention a so-called ABA problem, what is ABA, listen to me slowly tell you
Suppose this time now have two threads, thread thread 1 and 2 (the following referred to as “1, 2), 1 is 10 seconds to execute A thread, 2 is 2 seconds to perform A thread, so can perform is 10 seconds 2 5 times, it is obvious that fast 2 to 1, then 2 at this time from the main physical memory to take value, value A, 2 from their working memory will be changed to B, At this point, 1 goes to fetch the value of the main physical memory. It looks the same and gets the value of A successfully, but in fact it has been changed by 2 for several times. 1 is unaware of this, although the result remains unchanged, what will be the consequence? Think about it, a thief who steals someone’s money and then gives it back, the same money, your wife who cheats on you and then comes back, the same wife? ABA problems, too, can cause a lot of problems if not addressed. The most common is money problems, where someone embezzled your money and paid it back before you knew it. But others have already broken the law. As shown in the figure:
There is a solution to the ABA problem by adding the version number when changing the value: