Reputation: 539
I'm trying to understand the Java Memory Model, but I'm having a bit of trouble regarding actions. I understand the definition of an action as <t, k, v, u>, but I don't quite understand how a program is broken into actions, and how abstract these actions are.
My first assumption was that actions are atomic. var1 = var2
would be broken into two actions - a read of var2
and a write to var1
, but an example here suggests that var1 = var2
is an action itself. So each action corresponds to a statement in the source code.
How does this work with an if statement? For example, if we had if(r1 == r2 && r3 == r4) { ... }
, would the entire statement be one action, or broken into multiple actions (and if so, how do the corresponding actions stay 'connected' as an if statement)?
Upvotes: 5
Views: 130
Reputation: 43391
Actions are defined in JLS 17.4.2, which particularly focuses on "inter-thread actions:"
An inter-thread action is an action performed by one thread that can be detected or directly influenced by another thread. ... This specification is only concerned with inter-thread actions.
The full list of those actions is at the link (and not really worth copying in full here), but it includes reads and writes of variables.
The question of var1 = var2
depends on what var1
and var2
are. If they're both fields on a class, then those are two separate actions (the read of var2
and write to var
). If either of them is a local variable, then its state belongs to the method's stack frame, which is inherently thread-local and thus not an inter-thread action.
For instance, if var2
is a field and var1
is a local variable, then the only inter-thread action is the read of var2
— the write of that value to the local variable is not an inter-thread action, since it can only be observed by the thread that the local stack frame belongs to.
Similarly, if (r1 == r2 && r3 == r4)
can be up to 4 actions: the reads of the four variables, if they are fields. The results of the ==
and &&
operations are thread-local, and thus don't count as inter-thread actions.
Upvotes: 3