????Java????淶???????????ü??????????????湲????????????п?????????????????????????????????????????????????????
????????????????????????????????????????????????????????????????仯??
??????volatile??????????VM?????????????????????????????п???????????????????????????
??????y??飺????????????????????????????????volatile?????????????????synchronized??????У???????????????????á?
???????????volatile???ε???VM?б??????????????????Ч??????????????????????????????
????Java??serialization????????????????????????????????????????????????????????????????????
??????serialization???????????????????????????????????????serialization??????????????????????transient??
????transient??Java?????????????????????????????л?????????????????????л??????transient??????????????????л??????У??????transient???????????????????
???????static????????????л???
????Java ?????е? volatile ?????????????????? “??????? synchronized”???? synchronized ??????volatile ????????????????????????????????????????????????????????? synchronized ??????????????????????Ч??? volatile ??????????????????????????? volatile ?????????Ρ?
??????????????????????????mutual exclusion?? ???????visibility?????????????????????????????????????????????????????????????Э??????Э?飬????????????????????????ù??????????????????????Щ??????????????????????????????????????????????????????????????? —— ????????????????????????????????????????????????????????????μ?????????????????????
????Volatile ????
????Volatile ???????? synchronized ????????????????????????????????????????????? volatile ???????????Volatile ????????????????????????????????????????????????????????????????????????????????????????????????????? volatile ???????????????????????????κξ?????????????????????Invariants?????????? “start <=end”????
????????????????????????????????????????? volatile ??????????????????? volatile ??????????????Щ????÷???idiom???????????????????????volatile ?????????????????????????????????????????????????????Щ????£????????????????д??????volatile ?????????????????????????????
?????????? volatile ??????????
?????????????????Щ????????? volatile ????????????? volatile ??????????????????????????????????????????
???????????д?????????????????
???????????а???????????????????????С?
????????????Щ???????????????д?? volatile ????????Щ??Ч????????κγ????????????????????????
???????????????????? volatile ??????????????????????????????????????x++??????????????????????????????????????????????д??????????????????????????????????У??? volatile ?????????????????????????????????? x ?????????????????? volatile ????????????????????????????????????????д????????????????????????
???????????????ζ??????????????????????????????? volatile ?????????? synchronized ????????????????????????嵥 1 ??????????????????????Χ?????????????????? —— ???????С????????硣
?????嵥 1. ??????????????Χ??

 

@NotThreadSafe
public class NumberRange {
private int lower?? upper;
public int getLower() { return lower; }
public int getUpper() { return upper; }
public void setLower(int value) {
if (value > upper)
throw new IllegalArgumentException(...);
lower = value;
}
public void setUpper(int value) {
if (value < lower)
throw new IllegalArgumentException(...);
upper = value;
}
}

?????????????????Χ?????????????? lower ?? upper ??ζ???? volatile ????????????????????????????????????????????????????????????????????ò???μ????? setLower ?? setUpper ???????????Χ???????μ????????磬?????????? (0?? 5)????????????? A ???? setLower(4) ??????? B ???? setUpper(3)?????????????????????????????????????????????????????????????????????飬??ú???Χ??? (4?? 3) —— ?????Ч???????????Χ???????????????????? setLower() ?? setUpper() ???????? —— ??????ζ???? volatile ????????????????????
???????????
??????? volatile ?????????????????????????Щ?????£???? volatile ????????????????????????? volatile ????????????????????Щ????£?volatile ?????????????????????????
????????????????????????????? “X ????? Y ??”????????? JVM ??????????????????磬?Щ????? VM ?????????????????????????????????????? volatile?? synchronized ????????????????????????????????????volatile ??????????????? —— ??????? volatile ????????????? volatile д????????????? volatile д?????????????????????????????綨??Memory Fence????????????volatile ?????????????????????
????volatile ??????????????????????????????????????? volatile ??????£?volatile ???????Щ???????????????????????????????????????д??????????????volatile ????????????????????????????
?????????? volatile ????
????????????????????????????????? volatile ??????????????????????????????????????????????????????Щ?????????????????????????????? volatile ???????????μ???? volatile ?????? —— ?????????????????????????????????????? volatile —— ?????????????????Щ????????????????????
?????? #1???????
?????????? volatile ??????淶??y??????????????????????????????????????????????????????????????????????????
?????????ó????????????????????? “????????????????????????Щ????”?????嵥 2 ?????