java concurrent learning - Chapter X java memory model of semantic memory

A lock memory semantics

  java memory model is the semantic memory refers to the realization of the principles in the JVM.

  Semantic memory lock: Lock in addition to the critical section mutex execution, but also allows the release of the lock thread sends a message to acquire the same lock thread.

  We then finishing at the top of this sentence:

  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. 

  Lock achieve semantic memory:

  synchronized、ReentrantLock;

Two, volatile memory semantics

  volatile memory and the key memory semantics Semantic met:

  1. When writing a volatile variable, java memory model will now corresponds to the thread local memory shared variables flushed to main memory.

  2. When reading a volatile variable, java memory model corresponding to the current thread will memory shared variable is deasserted, and then read the shared variables in main memory.

  We can see volatile memory write and release the lock semantic memory semantics similar, volatile reading memory and semantic memory semantics similar to acquire the lock.

Three, final semantic memory

  final domain of semantic memory is to study their instruction reordering rules, divided into write and read both instructions reordering rules

  1. Write command field final reordering rules

  ① write final fields reordering rules prohibit the writing to the final reordering domain outside constructor

  ② static code block can not be assigned to constants, because the static code block is generated when the class loading, class initialization constants are created. We know that the loaded class, loaded with the first initialization, not all.

  ③ After the compiler to write the final fields, before the constructor completes, insert a memory barrier store store, to ensure that it is the final write operation is performed in the domain configuration method.

  2. Read the final field reordering rules

  In one thread, and the first initial read read object references final fields included in the object, java processor memory model prohibition rearranging these two operations.

  
  

  

  

Guess you like

Origin www.cnblogs.com/daijiting/p/11787649.html