Java????????????????
???????????? ???????[ 2016/5/10 14:22:54 ] ??????????????????? JAVA
?????????????
????????????????????????????????????????????塣???仰???????β???????????????????ж???????????????е????????????????????????????????????????????????????β???????????????????????????????????????????????????????????????ɡ????磬??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 ????????е???????????????????Щ???????????????л?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????У???????????к????????
??????
???·???
??????????????????
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