Java memory model (Java Memory Model, JMM)

Today what is called a simple chat Java memory model, not the JVM memory structure, oh.

JMM is a language-level memory model, processor hardware model is the hardware level, Java memory model is the basic guarantee visibility of memory. We volatile memory so as to achieve visibility provides the cornerstone. The main purpose is to allow Java programmers to achieve consistent results in a variety of access platforms

JMM decides a thread writes to shared variables when another thread is visible , especially in reading and writing to shared variables, immediately after the modification of other threads in the read, this is the main role in the JMM. From an abstract point of view, JMM defines an abstract relationship between the thread and the main memory: shared memory variables between threads in main memory (Main Memory), each thread has a private local memory (Local Memory) , local memory stored a copy of the thread to read / write shared variables. JMM local memory is an abstract concept, and are not real. It covers the cache, write buffer, registers and other hardware and compiler optimizations. Abstract Java memory model is schematically shown in FIG.

image

Speaking in front of our underlying principle volatile explain, this realization is the use of semantic Java memory model. For us to shield the details to achieve the visibility of memory, interested readers can see the article made public early numbers underlying principle volatile achieve .

Why JMM?

1. reordering sequence of instructions from source code into

When executing the program, in order to improve performance, often with a processor compiler will reorder instructions optimized to ensure the execution result with the result of execution of the program is consistent with the book, but does not guarantee that the code sequence input to the individual statements executed the same order. (Of course, say that in the case of single-threaded).

Volatile so there will not be a modified instruction code rearranged, equivalent to adding a memory barrier, not the subsequent instructions before reordering memory barrier.

2. Happens-before principle (first occurrence)

Happens-before defined:

  • If a Happens-before further operation of the operation, the execution result of the operation of a second operation will be visible, but the order of execution of the first operation before a second operation.
  • Happens-before relation is present between the two operations does not necessarily mean the order specified execution codes. If the results of the implementation of reordering consistent with the results performed in accordance Happens-before result, it is possible.

Occurs first partial order relation between two operations defined in the Java memory model, if the first operation A to B occurs, in fact, that the operation occurs before B, operator A can be produced by B was observed, "Influence "including modifying the values ​​of shared variables in memory, send a message, calling the method. such as:

//在线程 A 执行  1
i =  2;`
// 在线程 B 执行   2
j = i;`
// 在线程 C 执行   3
i =  3;
  • Suppose execution order 1 -> 3 -> 2: 3 is that the value of j.

  • Suppose execution order 1 -> 2 -> 3: 2 then the value of j is.

If we want to perform in accordance with 1, 2, 3 order. Only then synchronized to ensure consistency.

all in all:

  • By JMM memory model, volatile memory to achieve visibility was able to guarantee. But they do not need us to pay attention to the details of different platforms. When modification of a volatile variable is modified, JMM will corresponding to the thread local memory shared variables flushed to main memory.

  • When reading a volatile variable time, the thread will JMM local memory corresponding to an invalid setting, and reads the shared variable from main memory.

  • In the case of satisfying the principles volatile Happens-before, the disable command reordering.

Dear Readers feel good essays thumbs or public concern number will be my greatest support. Welcome to public concern number to obtain the latest technical articles.
JavaStorm.png

Guess you like

Origin www.cnblogs.com/uniqueDong/p/10944267.html