Java- final and volatile memory model of semantic memory

Premise: memory barrier

Memory barrier (Memory Barrier) and memory fence (Memory Fence) is the same concept. Instructions for preventing the reordering.

JMM memory barriers into four categories:

  • Store: will refresh the data processor cache memory.
  • Load: copies the data stored in the cache memory of the processor.
Barrier type INSTRUCTION Explanation
LoadLoad Barriers Load1;LoadLoad;Load2 The barrier to ensure that data is loaded prior to Load1 and Load2 all the load instruction operation
StoreStore Barriers Store1;StoreStore;Store2 The barrier to ensure Store1 immediately flush data to memory operations (make it visible to other processors) prior to all store instructions Store2 and subsequent operation
LoadStore Barriers Load1;LoadStore;Store2 Load1 ensure that all data is loaded prior to a store instruction Store2 and refreshing data in memory after the operation
StoreLoad Barriers Store1; Large Load; Load2 The barrier Store1 ensure data memory refresh immediately prior to the operation of all operating load after the load instruction and Load2. It will make the barrier before all memory access instructions (store instructions and access instructions) After completing Perform memory access instruction following the barrier

StoreLoad Barriers also have the effect of three other barriers, it is currently supported by most of the CPU. But compared with other barrier that the overhead is relatively expensive.

 

A, final

https://docs.oracle.com/javase/specs/jls/se8/html/jls-17.html#jls-17.5

When you declare a final member, you must set its value before the constructor exits.

Class Final Field Example {
     final  int x; 

    Public Final Field Example () { 
        x = 3 ;

Visibility

The compiler will write the modified variables for the final insert the memory barrier operation, final modified variables as immutable variable, as long as the object is the correct structure (not escape this happen), does not require any synchronization measures can ensure that any thread can after the read variable value is initialized in the constructor.

About this escape: https://www.jianshu.com/p/49068f29a460

Orderliness

final write: "write to the constructor of a final field", and "then this is constructed referenced object is assigned to a reference variable" between the two can not be re-ordering operation.

final read: "a first read-targeted final field contains a reference to" and, "and then the final read-targeted first field", the reordering is not between these two operations (first assignment, then call).

 

二、volatile

Atomicity

32 of the JDK volatile modified after long and double are also atomic. However, volatile int i = 0; i ++; not have the atoms, and therefore can be understood as having a single atomic read and write operations to volatile variables, the combined operation (e.g., i ++) is not atomic.

Visibility

Reader compiler to volatile variables of the operation into the memory barrier, modified volatile variables can be modified after being synchronized to the main memory immediately, which was modified from the variables are refreshed before each main memory is.

Orderliness

Also memory barrier may be limited to the volatile reordering processor.

 


https://www.jianshu.com/p/aa432a918db9

https://www.jianshu.com/p/157279e6efdb

Guess you like

Origin www.cnblogs.com/jhxxb/p/10944691.html