????????????
????volatile??Java????????????????????????????????У??????????????????????synchronized????synchronized??????????????????volatile????????????????synchronized?????????????????????????????????volatile???????????????
???????????????????????volatile?????????????????????????????????′???
public class TestVolatile {
boolean status = false;
/**
* ???л??true
*/
public void changeStatus(){
status = true;
}
/**
* ?????true????running??
*/
public void run(){
if(status){
System.out.println("running....");
}
}
}
???????????????????????????????????A???changeStatus()?????????B????run()???????????????"running....."??
????????NO!
??????????????????Щ??????????????????????????????????changeStatus???????????run????????????????????"running...."?????????????????У??????????????????????????????status????????A?????????????B????????"?????"????????????B?????????????status???????true????????????????
??????ν????????????????????????????????????????????????????????????????????????????????????????а??????????????
????Java??????
????????????????????????????????????JMM??java???????
????java???????????????????Java Memory Model??JMM????JMM???????ε????????????????????????????????java???????????????????μ???????Ч????
????JMM???????????????????д??????????????????JMM??????????????????????????????????洢???????(Main Memory)?У???????????????е??????棨Local Memory??????????汣???????????????????????????????????????????в??????????????????н??У???????????д??????е??????????????????????????

??????????????JMM?????????????????????ν???????棬????涼??????????????????????cpu???????????檔???????????????????????????????????ɡ?
????????????JMM?????????????????????????????????????????????????????????е?status?????A????????true????????????????A?????????У??????δ????????????????????B??????status?????false???????????й??status?????????????????????????????????????????????????????е?????????ν?????????????????????????????????synchronized????Lock??Щ??????????????е????????????????????????????volatile
????volatile??????????????????????????????????????????????????????????volatile??????????Ч???
????1.??д???volatile???????JMM??????????????????е?????????μ???????????
????2.???д?????????????????е??????Ч??
???????????????軔status?????volatile?????????????A????????true??????B??????????
????volatile boolean status = false;
???????????????
????????????????????????????volatile??synchronized?????????????????????????????Щ??????????й???????volatile????????????synchronized???????????????????????????£?volatile????????Ρ?????????????
package test;
import java.util.concurrent.CountDownLatch;
/**
* Created by chengxiao on 2017/3/18.
*/
public class Counter {
public static volatile int num = 0;
//???CountDownLatch?????????????????
static CountDownLatch countDownLatch = new CountDownLatch(30);
public static void main(String []args) throws InterruptedException {
//????30??????????????
for(int i=0;i<30;i++){
new Thread(){
public void run(){
for(int j=0;j<10000;j++){
num++;//??????
}
countDownLatch.countDown();
}
}.start();
}
//???????????????
countDownLatch.await();
System.out.println(num);
}
}
??????н????
????224291
????????????????Щ??????????????????volatile???ε????????????????????????????????300000??
???????????num++?????????????num++??????????????????????????????????????????????????????????????:
????1.???
????2.???
????3.???
???????????????????£??п??????A??num?????????????У???????????????????num???????????A?????????num????????????д???????У????????num????????????????С??30000??
???????num++???????????????
???????num++?????????????????????java???????е?????????????????????????CAS??????????????????
/**
* Created by chengxiao on 2017/3/18.
*/
public class Counter {
//???????????
public static AtomicInteger num = new AtomicInteger(0);
//???CountDownLatch?????????????????
static CountDownLatch countDownLatch = new CountDownLatch(30);
public static void main(String []args) throws InterruptedException {
//????30??????????????
for(int i=0;i<30;i++){
new Thread(){
public void run(){
for(int j=0;j<10000;j++){
num.incrementAndGet();//??????num++????????CAS???
}
countDownLatch.countDown();
}
}.start();
}
//???????????????
countDownLatch.await();
System.out.println(num);
}
}
??????н??
????300000
??????????????????????????????????????н????????????????
????????????????
????volatile?????????????????????????????
?????????????????????????????????????????????????н?????????????Ρ??????????????????????????
????1.???????????????????????????????????????????
???????磺a=1;b=a; ?????????У???????????????????????????????????????????????????????????????????????
????2.?????????????????????????????????????????3??????н??????????
???????磺a=1;b=2;c=a+b???????????????????a=1)??????(b=2)??????????????????????????????????????????c=a+b???????????????????????????????????????c=a+b=3??
??????????????????????????????????????????????????????£???????????????????????????????TestVolatile????????????????????????????a
public class TestVolatile {
int a = 1;
boolean status = false;
/**
* ???л??true
*/
public void changeStatus(){
a = 2;//1
status = true;//2
}
/**
* ?????true????running??
*/
public void run(){
if(status){//3
int b = a+1;//4
System.out.println(b);
}
}
}
???????????A???changeStatus?????B???run????????????4????b???????3???
?????????????????????п???b????2??????????????????????????ж????????????????????????????????????????е?1??2?????????????????????????п???????????????status=true?????a=2??????????B?????????4?????????A??a=2?????????δ????У?????b=a+1??????п??????????2??
???????volatile????????ι???????????????????????????volatile???ι????????????????????????????в??????????????????????????????????
????volatile????????????????Щ???????о???£?
????1.?????????????voaltileд????????????????????????????????????
????2.?????????????volatile??????????????????????????????????????
????3.?????????????volatileд??????????????volatile??????????????????
???????
??????????£?volatile????????????????????????????????????????????????????????????????????????????????????????????????volatile??????????????????/д????????????????num++????????????volatile???????????????????????????????????????ò??????е???????????????CAS?????????num++???????????????????????????????????????н????