Happens-Before the principle of JMM

  In java memory model, happens-before should be understood to: results from the previous operation, it may be acquired subsequent operation, i.e. memory visibility.
  In order to solve the visibility problem of memory multithreading, proposed the happens-before principle, so that adherence to these principles between threads, while optimizing compilers have also been certain constraints.
  The following are the principle elements:

1, program sequence rules

  Within a thread, the result of a piece of code execution is ordered. Instruction will still be rearranged, but no matter how the reordering, the result is generated in accordance with the order of the code will not change.

2, the tube locking rules

  Multithreaded environment, both in single-threaded environment or, for the same lock, one thread after unlocking the lock, another thread acquired the lock, you can see the result of the operation of a thread before.

3, volatile variable rules

  If a thread to go write a volatile variable, then a thread to read this variable, then the result of the write operation must be visible to read this thread.

4, the thread start rule

  A process executed in the main thread, the promoter thread B, then thread A promoter is visible before thread B with the modifications B thread shared variables.

5, the thread terminates rules

  A process executed in the main thread, the thread B sub-terminated, the modifications to the thread B is visible in the shared variable before terminating the thread A.

6, the thread break rules

  Call to thread interrupt () method first occurred in the interrupted thread code detection to interrupt event occurs, you can () detects whether the interrupt occurs through Thread.interrupted.

7, the transfer rules

  Happens-before principle is transitive, i.e., A happens-before B, B happens-before C, then A happens-before C

8, the object end rule

  Initialization completion is an object, i.e. the end happens-before it must finalize performed constructor () method

Guess you like

Origin www.cnblogs.com/ssl-bl/p/11076232.html