?????????????
????????????????????????????????????????????塣???仰???????β???????????????????ж???????????????е????????????????????????????????????????????????????β???????????????????????????????????????????????????????????????ɡ????磬??long??????????????????????????????λ???λ???в?????????ɡ??????磬??????????????? i++ ???????????????????????裺??1????????? i ???????2???? i ???м??????????3???????д????檔?????????????????????????????

????????????????е???????????????????????????????????????????????????????????Java?е?Synchronized??Lock??????????????ζ??????Synchronized??????????????????????????CAS??Compare And Swap????????????????????????????????????£??????£??????????????????????????У????????????????????????CAS???Щ?????2??????Ч??????????????????????????????????????????????£?CAS??????ж???
????????????
???????????????????JVM??????????????????JVM????????????????????????????

??????????????????????????????????????????????棨????CPU??????????????????????????????????С?洢????CPU?????????????????????????????????????ζ?????????????й?????????????д????????????????????и????????????????????????????????????????е??????????????????μ??????????????1??????????????????????2??п???????????1???????????????????????????γ???????????????2??????????
package com.paddx.test.concurrent;
public class VisibilityTest {
private static boolean ready;
private static int number;
private static class ReaderThread extends Thread {
public void run() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (!ready) {
System.out.println(ready);
}
System.out.println(number);
}
}
private static class WriterThread extends Thread {
public void run() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
number = 100;
ready = true;
}
}
public static void main(String[] args) {
new WriterThread().start();
new ReaderThread().start();
}
}
????????????????γ????????????100??ready?????????????????????????????????????????????????????????????????????????г?????????ε?????

???????????????????????п????????????????д????WriterThread??????ready=true????????ReaderThread????????????????????????false?????????????????????if (!ready)?????ж????д?????????????System.out.println(ready)????????д?????е???????????????????п??????????????????????Java ?п????Synchronized??Volatile???????????????????????????????з?????
?????塢?????
?????????????????????????????????????????????????????????????
??????1??????????????????????????????????????????????£????????°?????????????
??????2????????е????????????????????????????м?????Instruction-Level Parallelism?? ILP??????????????????С????????????????????????????????????????????????????
??????3????????????????????????????????/д????????????ü????洢???????????????????????С?
??????????????ο????JSR 133 ?ж??????????????????

???????????е??1???????????????????????? 1 ?????????? 3????С??????? 1 ????У?r2????????????? 4 ??д???????????? 3 ????У?r1????????????? 2 д????????????н??????????r2==2??r1==1???????????“??????”???μ??????????2???????????????????????????????????1?????2????????????????????????r2==2??r1==1??????Java ????????Synchronized??Volatile???????????
???????????
?????????Java ????????е???????????????????Щ???????????????л?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????У???????????к????????