Happens-Before Relationship in Java Memory Model

As for JLS ch17 Threads and Locks , it says: "if one action occurs - before the other, then the first is visible and ordered to the second"; Interesting:

(1) What does this mean by saying “ordered before”? Because even if action_a happens before action_b, action_a can be run after action_b in some implementation, right?

(2) If action_a occurs before action_b, does this action_a SHOULD NOT see action_b? Or action_a can see or not see action_b?

(3) If action_a will NOT happen before action_b, and action_b will NOT happen before action_a, does this mean that action_a may or may not see action_b?

(4) There can be no cyclical events - before, right?

Any answer would be appreciated :)

+6
source share
2 answers

(1) What does this mean by saying “ordered before”? Because even if action_a happens before action_b, action_a can be run after action_b in some implementation, right?

It happens before - this is a causal, not a temporary connection. action_a causally ordered to action_b , regardless of whether it executes before it or not. However, in practice, the lead time would be very difficult if there were no causal relationship without a temporary order. Check out my previous question , which details causality.

(2) If action_a occurs before action_b, does this action_a SHOULD NOT see action_b? Or action_a can see or not see action_b?

There is a certain general order of visibility of actions with each other. This is discussed in the section that lists well-formed executions. Therefore, for any two actions a and b, either a is visible for b, or b for a, or none of the above. A good understanding of understanding correct executions is Examples of the Java memory model: good, bad, and ugly .

(3) If action_a will NOT happen before action_b, and action_b will NOT happen before action_a, does this mean that action_a may or may not see action_b?

Yes it is possible. There is no guarantee.

(4) There can be no cyclical events - before, right?

Happens-before should be in partial order, and the key property for ordering is not a loop.

+6
source

What does this mean by saying “ordered before”? Because even if Action_a occurs before action_b, action_a can be performed after action_b in some implementation, right?

A It happens before the relation creates a memory barrier that prevents the -b action from happening before the -a action. Thus, some basic JVM optimizations cannot be applied. Thus, NO action-a cannot be performed after or together with action -b.

If action_a occurs before action_b, does that mean action_a MUST NOT see action_b? Or action_a can see or not see action_b?

This means that action-b should see all the changes caused by the -a action.

If action_a will NOT happen before action_b, and action_b will NOT happen before action_a, does this mean that action_a may or may not see action_b?

It happens before - a transitive relation. So, if action-a occurs before action -b, which occurs before action-c ... so that before action-y, and action-y occurs before action-z, then action-a occurs before action-z.

A occurs before the relation ensures that the actions that follow the current action see the changes made by the current action. If the changes are not visible, then a does not exist before.

There can be no cyclical events - before, right?

Right. If action-a occurs before action -b, action-c, action-d, then none of b, c, d can happen before action-a.

Edit:

JLS says it should be noted that having a relationship between two actions does not necessarily mean that they must be performed in that order in the implementation. If reordering leads to results compatible with legal enforcement, this is not illegal. So, if action-a takes place before the relationship with action -b, then action-b can be executed first if the final equivalent is sate, if action a was executed before action b. This is a specific implementation. JIT may decide to run action -b earlier than action a if this change in order does not affect the end result.

  1. Well, action-a is not dependent on action-b. at least theoretically :)

  2. Happens before sequential actions are indicated. If the actions are parallel, then a does not exist before.

Note. All this confusion is due to the fact that the deletion occurs earlier than the JIT, if there is no dependency between the two actions. Please read Escape Analysis .

+2
source

Source: https://habr.com/ru/post/980125/


All Articles