java interview -Java Memory Model (JMM)

One, two key issues concurrent programming

How to communicate how the synchronization between threads and between threads. java concurrent uses a shared memory model

Two, JMM memory model
Java memory model (i.e. Java Memory Model, referred JMM) itself is an abstract concept, not real, or a set of rules that describes the specification, the specification defines the set by the program of each variable (including the instance field, static fields and elements) constituting the array object access.

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), stored in the local memory of the thread read / write copy of the shared variable.

 

Three, JMM memory model characteristics

1. Visibility:

Since the program is run entity JVM thread, and each thread is created JVM will create a working memory (in some places called stack space) to store thread private data, and the Java memory model under which all variables are stored in the main memory, main memory is shared memory region, all threads have access, but the thread operations on variables (reading assignment, etc.) must be carried out in the working memory, the first variable you want to copy from main memory of their working memory space then the variable operation, after the operation is completed will write back to main memory variables , you can not operate the main variables in memory, working memory is stored in the main memory variable copy of a copy , as I said before, is a work memory for each thread the private data area, and therefore different threads can not access each other's work memory, a communication (by value) must be done by the main memory among threads, which follows FIG briefly visit:

To the communication between threads A and thread B, it must go through the following two steps.

1) A thread the updated local memory A shared variables to be flushed to main memory.

2) the thread B to read into main memory before the thread A has updated the shared variable.

2, atomicity

3, orderliness

Source code -> compiler optimization rearrangement -> Parallel instructions rearrangement -> rearrangement memory system -> command executed last

Single-threaded environment to ensure consistent implementation of the final results of the implementation of the program results and code sequence.
 
The processor performing the rearrangement, must be considered a direct instruction data dependencies
Multi-threaded environment alternately thread execution, because the compiler optimization rearrangement exists, the variables used in the two threads can ensure consistency can not be determined.
 

 

Prohibition instruction rearrangement section:
volatile achieve a ban on instruction reordering optimization, in order to avoid the phenomenon of program order execution of multi-threaded environment.
Memory barrier (Memory Barrier). Memory barriers, known as memory fence instruction is a CPU, which has two effects
One is to ensure the implementation of the order of certain operations.
Second is to ensure visibility of memory variable (using this characteristic to achieve the volatile memory visibility)
 
Since the compiler and a processor can execute instructions rearrangement optimization. If you insert a Memory Barrier between instruction will tell the compiler and CPU, no matter what instructions can not and this Memory Barrier instruction reordering. That is prohibited by inserting a memory barrier instructions on performing the reordering optimization before and after the memory barrier. Another effect is to force the Memory Barrier brush out all kinds of CPU cache data, so the thread can be read on any CPU to the latest version of the data.
 

JMM About synchronization rules:
a, before thread unlock the value of the shared variable must refresh back to main memory
b, before locking thread, you must read the latest value of main memory to their working memory
c, is the same lock lock unlock
 

Guess you like

Origin www.cnblogs.com/wjh123/p/11094622.html