What is it happens-before principle?

Java, happens-before principle, is proposed in JSR-133.

Original Abstract:

• Each action in a thread happens-before every subsequent action in that thread.
• An unlock on a monitor happens-before every subsequent lock on that monitor.
• A write to a volatile field happens-before every subsequent read of that volatile.
• A call to start() on a thread happens-before any actions in the started thread.
• All actions in a thread happen-before any other thread successfully returns from a join() on that thread.
• If an action a happens-before an action b, and b happens before an action c, then a happensbefore c.
• the completion of an object’s constructor happens-before the execution of its finalize method (in the formal sense of happens-before).

 

Translation coupled with his understanding is:

  • Program sequence rules: in a thread, according to a program order of the code book EDITORIAL write operation occurs after the first operation. (Here, CPU instruction involves rearrangement, it is necessary to add a memory barrier to ensure orderly)
  • Tube-lock rules: unlocking operation of a lock, first lock occurs subsequent to the operation of the lock. Here it must be emphasized that the same lock.
  • volatile variable rules: Write operations on a volatile variable read ahead in the face of this variable occurred.
  • Thread start rule: start Thread object () method first occurrence in this thread every movement.
  • Thread join () rule: all operations threads is called join () method of the first occurred in the join () returns.
  • Transitivity rule: a first operation occurs in operation b, b occurs first operation to the operation c, the operation occurs in a prior operation c.
  • End of rule objects: initialization of an object is completed (constructor executes end) first occurred in its finalize () method.


 


Java interview questions summary, there is always a stuck you!

 

Guess you like

Origin www.cnblogs.com/ConstXiong/p/11688034.html