How do I understand the sync order, which is a complete order?

JLS 17.4.4 says:

Each performance has a synchronization order. Synchronization order is the complete order for all execution synchronization activities.

I know what the general order is. And I know to synchronize actions. But what was confusing me is that this is the complete order for ALL sync actions. For example, an unlock action occurs on monitor m - before all subsequent locking actions on m . But if at the same time, there is a lock action on monitor n , does it need to be ordered after the unlock action on monitor m ? If not, why are all synchronization actions in the same order? Thank.

+3


source to share


1 answer


Great question. Although I don't know for sure the answer, the explanation that I think works best for my understanding, yes, there is a "sync order" between two different locks, because JLS 17.4.2 defines the actions described for ordering by "sync order" as having cross-threading (and thus one thread must dispatch an action before the other receives it).

However, here's the catch:

Synchronization actions induce synchronized communication with actions, defined as follows [...]



Only actions ordered by synchronization produce the desired / desired memory effects (i.e., advance the state of the cache).

So, basically, although all your "sync actions" follow the "sync order" (and thus the "general order"), only those that are synchronized with templates are valid for thread safety.

+1


source







All Articles