Valid reorders under the new JMM

I just wanted to know if the following numbers are valid or not in the new JMM

Original Code: instanceVar1 = value ;// normal read operation, no volatile synchronized(this) { instanceVar2 = value2; //normal read operation, no volatile } instanceVar3 = value3; //normal read operation, no volatile 

The above code can be reordered into the following executions.

 Case 1: synchronized(this) { instanceVar2 = value2; //normal read operation, no volatile instanceVar1 = value ;// normal read operation, no volatile } instanceVar3 = value3; //normal read operation, no volatile 

Another case:

 Case 2: synchronized(this) { instanceVar3 = value3; //normal read operation, no volatile instanceVar2 = value2; //normal read operation, no volatile instanceVar1 = value ;// normal read operation, no volatile } 

Another case:

 Case 3: instanceVar3 = value3; //normal read operation, no volatile synchronized(this) { instanceVar2 = value2; //normal read operation, no volatile instanceVar1 = value ;// normal read operation, no volatile } 

Another case:

 Case 4: instanceVar3 = value3; //normal read operation, no volatile synchronized(this) { instanceVar2 = value2; //normal read operation, no volatile } instanceVar1 = value ;// normal read operation, no volatile 

All 4 cases listed above are valid for reordering source code in the new JMM model. I gave all the above reorders based on my understanding of http://gee.cs.oswego.edu/dl/jmm/cookbook.html

+4
source share
1 answer

Consider how normal loading / stocks are reordered when the monitor enters and exits:

Case 1 reorders the normal load / storage with the monitor input, which is the actual reordering.

Case 2 reorders the normal load / storage with monitor input and monitor output, followed by normal load / save, which are valid reorders.

See a similar example: Roach Motels and the Java memory model . This shows that access can be moved to the synchronized block, but not returned again.

Case 3 and 4 reorders the monitor input, followed by normal load / storage, which is invalid.

+1
source

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