Happens-Before relation in Java Memory Model

前端 未结 2 1305
广开言路
广开言路 2021-01-05 17:36

Regarding JLS ch17 Threads and Locks, it says \"if one action happens-before another, then the first is visible to and ordered before the second\"; I wonder:

(1) Wha

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-05 18:01

    (1) What does it really mean by saying "ordered before"? Because even if action_a happens-before action_b, action_a can be executed after action_b in some implementation, right?

    Happens-before is a causal, not a temporal relationship. action_a is causally ordered before action_b, whether or not it actually executes before it. In practice, however, a runtime would have a really hard time maintaning the causality without temporal order. Check out my earlier question which goes into some detail on the subject of causality.

    (2) If action_a happens-before action_b, does it mean action_a MUST NOT see action_b? Or action_a may see or may not see action_b?

    There is a definite overall order of the visibility of actions to one other. This is dealt with by the section specifying well-formed executions. Therefore, for any two actions a and b, either a is visible to b, or b to a, or none of the above. A good read to understand the notion of well-formed executions is Java Memory Model Examples: Good, Bad, and Ugly.

    (3) If action_a does NOT happen-before action_b, and action_b does NOT happen-before action_a, does it mean action_a may see or may not see action_b?

    Yes, either is possible. There is no guarantee either way.

    (4) There could not be any cyclic happens-before, right?

    Happens-before must impose a partial ordering, and the key property of ordering is no loops.

提交回复
热议问题