????volatile ??????
??????漸???????????????????????????????Щ??????? volatile ??????ò????????????????????????????????????У?volatile ?????????????????????
????volatile ?????????????????????????????????????????????????Щ???????????????????????????С??????????????????????????????? volatile ??????????????????????????????????????????????????????????????????檔???????Щ??????????????????????????????????????????? —— ??????????????????????????????????????????????????????????????????????????????????????????ò??????????????????????????????????
?????? #5?????????????д??????
???????????????????? volatile ????????????????????????? ++x ?????????????????????????洢???????????????????????????? volatile ???????????????????????????????п???????
????????????????????????д???????????????????????? volatile ?????????????????·??????????嵥 6 ?????????????????????? synchronized ??????????????????????? volatile ??????????????????????2??????????÷??????????????????????·????????????漰 volatile ????????????????????????????????????????
?????嵥 6. ?????? volatile ?? synchronized ??? “???????????д??”
?????嵥 6. ?????? volatile ?? synchronized ??? “???????????д??” ?? 6. ?????? volatile ?? synchronized ??? “???????????д??”

 

@ThreadSafe
public class CheesyCounter {
// Employs the cheap read-write lock trick
// All mutative operations MUST be done with the 'this' lock held
@GuardedBy("this") private volatile int value;
public int getValue() { return value; }
public synchronized int increment() {
return value++;
}
}

????????????????????? “???????????д??” ?????????????????????????ж?д??????????????е?д????Υ??????? volatile ????????????????????? volatile ????????????? —— ???????????????????????????????????? volatile ????????????????????????????????б仯?????????? volatile ????????????????У???????????????????????volatile ???????????ж?????????????? volatile ?????????·???????????????????????·????????????? —— ?????д???????????????????μ??????????????????????????????????????????????????????????÷???????
??????????
????????????Volatile ????????????????????????????????????????Щ????????????????????????????????????? volatile ????????? —— ??????????????????????????????????? —— ???Щ??????????? volatile ???? synchronized ???????????????? volatile ?????????????????????????????????????????????????????? volatile ???? synchronized ????????Щ???????????Щ?????????????????????????????????????????????????????????? volatile ???????????????