??????????????????????????????????????????????Ч???????????????????????thread interference?? ?? ???????????memory consistensy errors????
??????????????????????? ????????thread synchronization???????????????????????????????????????
???????????????????????????????
?????????
????????????????????????????????????????????????????? (Thread interference)
?????????ζ????????????????????????????????????????У????2??輕??
???????????
??????????????? Counter
????<code class="java">class Counter {
????private int c = 0;
????public void increment() {
????c++;
????}
????public void decrement() {
????c--;
????}
????public int value() {
????return c;
????}
????}</code>
????????????Counter?е?????????????????????increment()?????У??? statement ??c++??
????????????????????? statement????JVM???????????????? —— ???? statement ????????
??????????? statement c++?????????????? ( c--??? )
??????????c???
????????????????1
?????????????洢??c??????????
???????裬???A????increment()?????????B????decrement()??????????????????????????
????Thread A: ??? c.
????Thread B: ??? c.
????Thread A: ???????????? 1.
????Thread B: ???????????? -1.
????Thread A: ?????????洢?? c; c == 1.
????Thread B: ?????????洢?? c; c == -1.
???????A??????????????B????????????????????????????????B??????A?????????н?????
?????????????? —— ????? ????????????bug???????????????