问题描述
我只想知道下面的重新排序在新的JMM模式下是否有效。
原始代码:
instanceVar1 = value; //正常读操作,没有volatile
synchronized(this){
instanceVar2 = value2; //正常读操作,没有volatile
}
instanceVar3 = value3; //正常读操作,没有volatile
上述代码可以重新排序为以下执行。 p>
案例1:
synchronized(this){
instanceVar2 = value2; //正常读操作,没有volatile
instanceVar1 = value; //正常读操作,没有volatile
}
instanceVar3 = value3; //正常读操作,无变化
另一种情况:
情况2:
synchronized(this){
instanceVar3 = value3; //正常读操作,没有volatile
instanceVar2 = value2; //正常读操作,没有volatile
instanceVar1 = value; //正常读操作,没有volatile
}
另一种情况:
情况3:
instanceVar3 = value3; //正常读操作,没有volatile
synchronized(this){
instanceVar2 = value2; //正常读操作,没有volatile
instanceVar1 = value; //正常读操作,没有volatile
}
另一种情况:
案例4:
instanceVar3 = value3; //正常读操作,没有volatile
synchronized(this){
instanceVar2 = value2; //正常读操作,无挥发
}
instanceVar1 = value; //正常读操作,无挥发
$ b b
上述4种情况都是在新JMM模型下原始代码的有效重新排序。
根据我对
的理解,我已经给出了所有上述的重新排序。
考虑如何正常加载/存储与监视器重新排序输入和退出:
案例1使用监视器输入重新排序正常加载/存储,这是有效的重新排序。 p>
案例2使用监视器输入重新排序正常加载/存储,后跟正常加载/存储的监视器退出,这是有效的重新排序。
查看类似的示例:。
案例3和4重新排序监视器输入,后跟正常加载/存储,不是有效。
I just wanted to know whether the below reorderings are valid one or not under new JMM model
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
Do all the above 4 cases are valid reordering of original Code under new JMM Model.I have given all the above reorderings based on my understanding ofhttp://gee.cs.oswego.edu/dl/jmm/cookbook.html
Consider how the normal load/stores are reordered with the monitor enter and exits:
Case 1 reorders a normal load/store with a monitor enter which is a valid reordering.
Case 2 reorders a normal load/store with a monitor enter, and a monitor exit followed by a normal load/store, which are valid reorderings.
See a similar example: Roach Motels and Java Memory Model. This shows that accesses can be moved into a synchronized block but not back out again.
Case 3 and 4 reorders a monitor enter followed by a normal load/store which is not valid.
这篇关于有效的重新排序 - 在新的JMM下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!