???????????A???writer()??????????B???reader()??????????happens before????????????????happens before ?????????????

????1???????????????1 happens before 2; 3 happens before 4??

????2??????volatile????2 happens before 3??

????3??????happens before ??????????1 happens before 4??

????????happens before ???????λ???????????£?

??????????У?????????????????????????????happens before ?????????????????????????????????volatile??????????????????Щ?????????happens before?????

????????A???д???volatile??????B????????volatile??????A?????дvolatile?????????п??????????????B????????volatile????????????????B???????

????volatileд-???????????

????volatileд????????????£?

?????? ??д???volatile???????JMM??????????????????е?????????μ?????檔

?????????????????VolatileExample????????????A???????writer()????????????B???reader()?????????????????????????е?flag??a??????????????????A???volatileд???????????????????

?????????????????A??дf(shuō)lag?????????????A?б????A???1?????????????????????μ???????С?????????????A????????е??????????????μ??

????volatile??????????????£?

?????? ???????volatile???????JMM??????????????????????Ч????????????????????ж???????????

?????????????B??????volatile???????????????????????

?????????????????flag?????????????B??????????Ч??????????B???????????ж??????????????B?????????????±??????B????????е????????????????μ????

????????????volatileд??volatile????????????????????????????????B?????volatile??????д???A??д???volatile?????????п?????????????????????????????B?????

?????????volatileд??volatile??????????????????

?????? ???Aд???volatile????????????????A???????????????volatile?????????????????????????????????????????

?????? ???B?????volatile????????????????B??????????????????????д???volatile????????????????????????????

?????? ???Aд???volatile????????????B?????volatile???????????????????????A?????????????B?????????