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

时间:2020-12-30 10:54:01   收藏:0   阅读:0

原文链接: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关系:

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!