码迷,mamicode.com
首页 > 移动开发 > 详细

Understanding Happens-before relationship(理解Happens-before关系)

时间:2020-12-30 10:54:01      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:stand   有一个   until   volatile   java   enter   memory   abs   可见   

原文链接:https://www.logicbig.com/tutorials/core-java-tutorial/java-multi-threading/happens-before.html

Happens-before relationship is a guarantee that action performed by one thread is visible to another action in different thread.
Happens-before关系是一种保证,它保证一个线程的实施的动作对另一个线程是可见的。

Happens-before defines a partial ordering on all actions within the program. To guarantee that the thread executing action Y can see the results of action X (whether or not X and Y occur in different threads), there must be a happens-before relationship between X and Y. In the absence of a happens-before ordering between two operations, the JVM is free to reorder them as it wants (JIT compiler optimization).
Happends-before定义了一种“偏序”的排序方法作用于一个程序内部所有动作。为了保证动作Y能够看到动作X产生的结果(不管两者是否在不同线程中),那么必须有一个happends-before关系存在于X和Y之间。对于在没有happens-before关系的两个动作,JVM可以重排两个动作(比如在JIT编译优化的时候)。

Happens-before is not just reordering of actions in ‘time‘ but also a guarantee of ordering of read and write to memory . Two threads performing write and read to memory can be consistent to each other actions in terms of clock time but might not see each others changes consistently (Memory Consistency Errors) unless they have happens-before relationship.
Happens-before不仅仅是对动作做出及时的重排序,它还对内存读写的顺序做出保证。两条线程依照着时钟实施对内存的写和读是能够相互保证一致性的,但他们可能不能看不到对方的变化而和对对方保持一致,除非他们有happens-before关系。

How to establish happens-before relation? 如何确立HB关系?

Followings are the rules for happens-before:
接下来的原则是为HB关系准备的,满足了任何一条原则,那么就能建立起HB关系:

  • Single thread rule: Each action in a single thread happens-before every action in that thread that comes later in the program order.

  • 单线程原则:在同一个线程中,书写在前面的操作happen-before后面的操作。
    技术图片

  • Monitor lock rule: An unlock on a monitor lock (exiting synchronized method/block) happens-before every subsequent acquiring on the same monitor lock.

  • 锁原则:同一个锁的unlock操作happen-before此锁的lock操作。
    技术图片

  • Volatile variable rule: A write to a volatile field happens-before every subsequent read of that same field. Writes and reads of volatile fields have similar memory consistency effects as entering and exiting monitors (synchronized block around reads and writes), but without actually aquiring monitors/locks.

  • Volatile变量原则:对一个volatile变量的写操作happen-before对此变量的任意操作(当然也包括写操作了)。
    技术图片

  • Thread start rule: A call to Thread.start() on a thread happens-before every action in the started thread. Say thread A spawns a new thread B by calling threadA.start(). All actions performed in thread B‘s run method will see thread A‘s calling threadA.start() method and before that (only in thread A) happened before them.

  • 线程启动的happen-before原则:同一个线程的start方法happen-before此线程的其它方法。
    技术图片

  • Thread join rule: All actions in a thread happen-before any other thread successfully returns from a join on that thread. Say thread A spawns a new thread B by calling threadA.start() then calls threadA.join(). Thread A will wait at join() call until thread B‘s run method finishes. After join method returns, all subsequent actions in thread A will see all actions performed in thread B‘s run method happened before them.

  • 线程join原则:假设A执行join的线程B,B中的所有动作都happen-before A的位于此次join之后的动作。
    技术图片

  • Transitivity: If A happens-before B, and B happens-before C, then A happens-before C.

  • happen-before的传递性原则:如果A操作 happen-before B操作,B操作happen-before C操作,那么A操作happen-before C操作。

Understanding Happens-before relationship(理解Happens-before关系)

标签:stand   有一个   until   volatile   java   enter   memory   abs   可见   

原文地址:https://www.cnblogs.com/ralgo/p/14186344.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!