XII-depth understanding of the Java Memory Model

In-depth understanding of the Java Memory Model

[1] CPU and cache consistency

img

We should all know that computers in the implementation of the program, each instruction is executed in the CPU, and the implementation of the time, they will inevitably have to work with data. The above data and the computer, which is stored in the main memory, i.e. the physical memory of the computer it.

At first, still live in peace, but with the development of CPU technology, the execution speed of the CPU faster. And because memory technology has not changed much, so the execution speed read and write data from memory and CPU compared to the process of the gap will be increasing, which causes the CPU to the memory of each operation has to spend a lot of waiting time.

So, people came up with a good way is to increase the cache between the CPU and memory. The concept of caching we all know, is to save a copy of the data. He is characterized by fast, small memory, and expensive.

Then, execution of the program becomes:

When the program is running, the operation will need data from the CPU to the main memory copy of the cache which then calculates the CPU can directly write its cache to read data from and wherein the data, when after the end of the operation, then the data cache to the main memory to refresh them.

Between the CPU and main memory to increase the cache may exist in a multi-threaded scenarios cache coherency problem , that is, in the multi-core CPU, each core its own cache, the cache content on the same data may inconsistent.

[2] and the optimization processor instructions rearrangement

In the above-mentioned between the CPU and main memory cache increases, there will be a multi-threaded scenarios cache coherency problem . In addition to this case, there is a hardware problem is also more important. That is to bring the inside of the processor computing unit can be fully utilized as much as possible, the processor may be executed out of order code input process. This is the processor optimization .

In addition to current popular processors will optimize the code out of order processing, many programming language compilers will have a similar optimization, such as the Java virtual machine time compiler (JIT) would do the command rearrangement .

One can imagine that if allowed to optimize processor and compiler instruction reordering, then it could lead to various problems.

Solution: memory barrier

[3] What is the Java Memory Model

Memory model

To ensure the shared memory (visibility, order, atomicity), shared memory model defined specifications in the memory system read and write operation of a multithreaded program behavior. These rules regulate read and write operations to memory, so as to ensure the correctness of execution.

Java memory model (Java Memory Model, JMM) is a specification defined by the java virtual machine, used to mask out differences java program in a variety of different hardware and operating system access to the memory, and this can be achieved in a variety of different java program to achieve uniform memory access on the platform.

img

The main objective of the Java Memory Model is to define variables in a program access rules. I.e., in the virtual machine in a variable stored in the main memory or the low-level details of such variables extracted from the main memory. Note that variable here with us write java program variables are not exactly the same. Variable here is the instance fields, static fields, the elements constituting the array object, but not including local variables and parameters of the method (because it is private to the thread).

Java memory model concepts involved are:

  • Main memory: java virtual machine requires all variables (variables are not in the program) it must be generated in the main memory. Can be compared to the physical main memory in front of that machine, but the machine's main memory is the entire physical machine's memory and the main memory of the virtual machine is part of a virtual machine's memory.
  • Working memory: java virtual machine, each thread has its own working memory, the memory is thread-private. Comparable in front of that cache. Memory worker threads need to save a copy of the variable in the main memory. VM provides the thread changes to the main memory variables must be in working memory thread can not be directly read and write the main variables in memory. Between different threads can not access each other's working memory. If the value of the variable is need to pass between threads, it must be passed through the intermediary of the main memory.

Working memory and the main memory is divided and the Java heap, stack, different zones dividing method, the relationship between the two substantially no, if forced correspond, the main memory heap instance be understood as part of the data, working memory corresponding to the partial region of the stack

About Java heap, stack, method area: Click on learning

[4] volatile memory semantics

volatile memory write semantics:
When writing a variable time, the private memory will JMM thread updates the value of the shared variable to main memory, and the value is set in the other thread to be invalid;
volatile memory semantics to read:
when reading a variable time, JMM first determines whether the value in the private space is invalid, if the failure, the next thread is read from main memory variable.

For details, see: Click to learn

[5] key memory semantics

When a thread releases the lock, JMM will corresponding to the thread local memory shared variables flushed to main memory.

When a thread to acquire the lock, JMM will the thread corresponding local memory is deasserted. So that the critical region of code protected by the monitor must be shared variable to read from the main memory.

For details, see: Click to learn

Guess you like

Origin www.cnblogs.com/lee0527/p/11729698.html