Depth understanding of java virtual machine: java memory model with thread

Below is the basic cause of the problem is complicated by inconsistencies generated:

 

 It is the instruction rearrangement optimization,

1, java memory model

java virtual machine attempts to define a set of java memory model specification, to block out the memory access a variety of hardware and operating system differences, in order to achieve the java program so that memory accesses can reach consistent results under a variety of platforms. The main objective is to model java memory access rules define the variables in the program, i.e., stored in memory and removed from memory such details in the virtual machine in the variable. java memory model specifies all the variables stored in the main memory, each thread also has its own working memory, storing a copy of a copy of the variables used by threads, threads for all variables must be in working memory, you can not directly access the main RAM. Different threads can not access each other working memory, variable values ​​are passed between threads through the main memory are completed. Thread, the relationship between the main memory, working memory as shown below:

 

In order to obtain higher speed, working memory priority will be stored in a register or tell the cache, the program runs the main access working memory. Virtual machine copy of an object, it will only copy references and variables used in the thread will not copy the entire object. In addition to storing heap data objects additional information for the virtual machine also stores hotspot markword (hash code stored objects, the GC flag, the GC age lock synchronization information) (pointer to the metadata storage type) klass point

 2, memory interaction between

For the main memory and working memory of the interactive protocol, how to copy a variable from main memory into the working memory, and operational details of how to synchronize back to main memory from the working memory, java memory model is defined as eight operations, eight operations are atomic inseparable.

1) lock acting on the main memory variable, the variable identified as occupying a thread

2) unlock the role of the main memory variable, the variable will lock release.

3) read a variable acting on the main memory, a variable read into the working memory, for subsequent load operations.

4) load acting on the work memory variable, the variable is read into a working memory copy

5) use to act on the work memory variable, it passes the value of memory to perform a variable engine, whenever the virtual encounters a bytecode instructions need to use when the variable value will perform this operation.

6) assign a variable acting on the working memory, the working engine receives the value assigned to variable working memory, to perform this operation every time the virtual encounters a bytecode instruction to the variable assignment.

7) store in working memory the effect of a variable value is transferred to the main memory in order to later write operation may be used.

8) write variables acting on the main memory, the variable store he obtained from the working memory into the main memory variable

read, load, store write the order and must appear in pairs. It is not allowed to discard the latest assign operation, i.e. the variable variable changes in the working memory values ​​must be synchronized to the main memory. A thread is not allowed to put the operation did not happen assign work memory synchronization to the main memory.

A new variable can only be produced in main memory, are not allowed to use variables (load and assign) operation is not initialized directly in memory is the use, you must load before and assign store. A variable can only be one thread lock, the same thread can perform the lock operation several times,

It must be the same number to unlock unlock after several lock. If, after lock on a variable, it would empty his work memory value of this variable, which is necessary to load or assing operation before execution engine to use this value. Must be synchronized to the working memory, such as main memory store, write is performed on a variable unlock

Guess you like

Origin www.cnblogs.com/xiaofeiyang/p/11979035.html