Defined
Java memory model, which defines how the JVM works on the computer's memory. The JVM is a virtual model of the entire computer, so JMM is subordinate to the JVM. If we want to learn more about Java concurrency programming, we need to understand the Java memory model first. The Java memory model defines the visibility of shared variables between multiple threads and how to synchronize shared variables when needed . The original Java memory model was not very efficient, so the Java1.5 version was refactored, and now the JAVA8 is still in the Java1.5 version.
The key to multithreading
Multi-threaded communication and synchronization (communication is multithreading to the same resource in parallel access, synchronization is the sequential access to this resource)
Variable storage
A local variable, if it is the original type, is completely stored in the stack area.
A local variable can also be a reference to an object, in which case the local reference is stored in the stack, but the object itself is still stored in the heap.
For a member method of an object, these methods contain local variables that still need to be stored in the stack area, even if they belong to the object in the heap.
For a member variable of an object, regardless of whether it is a primitive type or a wrapper type, it is stored in the heap area.
Variables of the static type and information about the class itself will be stored in the heap as the class itself.
——
Objects in the heap can be shared by multithreading. If a thread obtains an application for an object, it can access the member variables of the object. If two threads invoke the same method of the same object at the same time, the two threads can access the object's member variables at the same time, but for local variables, each thread copies one copy to its own line stacks.
——
JMM and Hardware Memory architecture
The Java memory model is inconsistent with the hardware memory architecture. Hardware memory architecture does not distinguish between stacks and heaps, from the hardware, whether stack or heap, most of the data will be stored in main memory, of course, some stacks and heap data may also be stored in the CPU register, as shown in the Java memory model and the computer hardware memory architecture is a cross-relationship:
Note:
The principle of the volatile keyword is that if a variable is volatile, JMM inserts a write-barrier instruction after the field is written, and a read-barrier instruction is inserted before the field is read. This means that if you write a volatile variable, you can guarantee that:
1. Once a thread has written to variable a, any thread accessing the variable will get the latest value.
2. Before writing to the variable A, its updated data is also visible to other threads. Because the memory barrier will brush out all previous writes in the cache.
——
Second, memory barrier (Barrier)
The memory barrier allows the reordering of specific types of processors to be suppressed, allowing the program to execute as we expect. The memory barrier, also known as the memory fence, is a CPU instruction, basically it is an instruction like this:
guarantees the order in which specific operations are executed.
Memory visibility that affects some data, or the execution result of an instruction.
Another thing that Memory Barrier does is to force the various CPU caches to be brushed out, such as a write-barrier (write barrier) that will flash all the data written to the cache before Barrier, so Any thread on the CPU can read the latest version of the data.
Java Memory Model-JMM