Java??????JMM???
??????????? ???????[ 2017/5/24 10:34:49 ] ??????????? ???????????
?????????????
????volatile?????????
??????????????getfield?? getstatic?? ??volatile?????arrayload?? ???д?putfield?? putstatic?? ??volatile?????arraystore??
????volatile??д?????volatile??ε?getfield?? getstatic??putfield?? putstatic??
????monitorenter????????????????????monitorexist???????????????????
????????????е?No??????????????????????????(???д?? volatileд)???volatile??ε?д?????????????volatile??ε?д?????????????No????????????????????????JVM??????С?????-???????????????????????????д????64λ??double??long??д??????????????????volatile?????????????????д????????????????????????????????????
????final???
????final?????????????????????
????1??final??ε?д??????????н??У????final??ζ???????????д??????????????????????????и?final??ε?????д???????????磬 ????????????????????
????x.finalField = v; ...; sharedRef = x;
????2??final??ε????μ?????????????final??ε?????д??????????????????????????????????
????x = sharedRef; ...; i = x.finalField
???????????
???????????????????????????(memory barrier)?????(fence)???????????????????????????????????????磬CPU??????д???????store???????write buffer?е??flush????棬???????????barrier??????????store???????????????????????????????????
???????????????
???????????е?????????????????????barrier??????????Fence(?????Χ?)??????????fence???????load??store??????????fence????load??store??????????????????????????????barrier
????LoadLoad Barriers
????Load1; LoadLoad; Load2;
???????Load1????????Load2??????load?????
????StoreStore Barriers
????Store1; StoreStore; Store2
???????Store1??????????Store2?????????? ???????????????
????LoadStore Barriers
????Load1; LoadStore; Store2
???????Load1????????????Store2??????????flush?
????StoreLoad Barriers
????Store1; StoreLoad; Load2
???????Store1??????????????????????(??flush?????)????Load2??????load???????????StoreLoad Barrier??????load???????????????????????????д????????
?????????????????е???????????StoreLoad??StoreLoad?????????????????StoreLoad?????????????????Ч????????StoreLoad??????????????(????????????)?????
?????????????????????????????????????????е??????????
??????????final??ε?????????final??д??????barrier
????x.finalField = v; StoreStore; sharedRef = x;
???????????????
??????????????????????volatile??Ρ?synchronized???????????????????????????????????
????1??volatile store?????StoreStore????
????2??????final???д???????????????????StoreStore
????3??volatile store?????StoreLoad????
????4????volatile load?????LoadLoad??LoadStore????
????5??monitor enter??volatile load??????£?monitor exit ??volatile store????????
????HappenBefore
??????????????????????????????????????????????JMM????????????HappenBefore??????????????????????????в???B????????????A??????????A??B?????????????????)?? ?????A??B???????????HappenBefore?????????JVM???????????????????
????HappenBefore?????б?
????HappendBefore???????
????1????????????: ????????в???A?????B???????????????в???A???????B??????
????2??????????????: ???????????????????????????????????????????????????
????3??volatile????????: volatile??????д???????????????????????????
????4????????????: ????????Thread.start????????????????????κβ????????
????5????????????: ????е??κβ?????????????????????????????????????
????6???ж????: ???????????????????????interrupt???????????ж??????interrupt?????
????7????????: ???????A?????B????У????????B?????C????У????????A?????C????С?
???????????????????????????????????壬????????volatile?????????????塣????????????volatile??????????д??????????????????????????volatile??д?????????volatile?????????С?
????????????????HappenBefore???????????????
???????????A?????????????????????????????????????????????HappenBefore?????????????????????????????HappenBefore??????????B???????????????????????????????HappenBefore?????B?е??????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11