????volatile????????
????1?????????????
????2????????????
?????????????volatile???????????????????
????????????????
????1 ????
????JVM????????????????????????????
????Java??????漲?????????????????????洢??????浱?У????????????????????????棨????CPU?????????????????????????????棬??????????????????????檔
????????????б?????????湲??????????????????????Щ???????????????????????????е????????????????????????????????浱?С?
??????α????????????????????????????????????Java????????漲?????????????????佻????Э?飬??????8??????????
????(1) lock:????????е??????????????????????
????(2) unclock:??lock????????????????????????????л??????????
????(3) read:????????е???????????????浱??
????(4) load:??read?????????浽????????е?????????С?
????(5) use:??????????????????????
????(6) assign:????????洦???????????????????????
????(7) store:??????????????洢????????С?
????(8) write:??store?洢???д?????????????????С?
???????????Java????????????????????????????????????????????????????????????????????????????????????????????е??????д??????棬??????????????????潫????????????????????????????????????????????????????????????????е??????????????????????o?????
????2 ????????????????
????????????????????????????????????????????????????????????????????????????????????????????????????
????????1
???????????????????open:
????boolean open=true;
?????????????????????????????????????true???????false???????????????????A????????Щ??????open????false:
????<strong>//???A
????resource.close();
????open = false;
???????B??????open????????open?true??????????????????????Щ????:
????<strong>//???B
????while(open) {
????doSomethingWithResource(resource);
????}
??????A????????????open?????????B???????????????open??????????δ????????B?????????У???????B???????????????????????Щ????????????????
????????2
????????????????????????ж?????????????????
public class CancelThreadTest {
publicstatic void main(String[] args) throws Exception{
PrimeGeneratorgen = new PrimeGenerator();
newThread(gen).start();
try
{
Thread.sleep(3000);
}finally{
gen.cancel();
}
}
}
class PrimeGenerator implements Runnable{
privateboolean cancelled;
@Override
publicvoid run() {
while(!cancelled)
{
System.out.println("Running...");
//doingsomething here...
}
}
publicvoid cancel(){cancelled = true;}
}
???????????????PrimeGenerator???????????????PrimeGenerator?????????????????????????????????cancelled??????????????????????cancelled????????????????????棬????PrimeGenerator????????????????????????????????????棬??PrimeGenerator??????????
???????PrimeGenerator???????Щ??????????????????????????????????????cenceled??volatile????????????????
?????????????????????????????????????????????????PrimeGenerator????а???BolckingQueue.put()????????????????????????????????cancelled?????????????????????????????????μ?????????????????????????????
????3 ?????????
????volatile??????????????????η???????????????????£??????η??????????????μ?汾??????volatile????????????????????????????????
????????????????1??
??????????????open?????????????????????????Java???????volatile????????????open????????????volatile??????????open????????????open????????е?????????????????
????????????????2??
??????cancelled????????volatile???????????cancelled?????????????PrimeGenerator????????????
???????????????????synchronized?????open??????Get/Set????????????????????????Get????open???????Set????????????????潫??????????????
?????????????????
????1 ????
???????????????JVM???????????????????Ч?????????????????н????????£?????????????ж???????????????????????????????????????????????????????????????????????????
???????????????????????????????????????????????????
????double r = 2.3d;//(1)
????double pi =3.1415926; //(2)
????double area = pi* r * r; //(3)
????area???????????r??pi????????????????r??pi???????????
????as-if-serial??????????????????????????????????????????ж??????????????????????????????????????Runtime????????????????????塣
???????????1?? – happensbefore -> ??2??????2?? – happens before -> ??3??????????????(1)(2)(3)??(2)(1)(3) ????r??pi??area????????????????????????Runtime??????????????????????1????2??????????????????????
????????????????????????????????????????
????2 ????????????????
??????????????????????????JVM???????????????濴?????????
????????1??A?????????????B??????
??????????????????????????????????????????????????????????????????????????????????????????:
?????????A??:
????context = loadContext();
????inited = true;
?????????B??:
????while(!inited ){ //???????A?ж?inited??????????????????context????
????sleep(100);
????}
????doSomethingwithconfig(context);
???????????A?з??????????????:
????inited = true;
????context = loadContext();
???????B?к?????????????δ?????????δ?????????context????????????????
????????2???????????μ??????Ч
??????????????????????????????????ж????????
????public class Singleton {
????private static Singleton instance = null;
????private Singleton() { }
????public static Singleton getInstance() {
????if(instance == null) {
????synchronzied(Singleton.class) {
????if(instance == null) {
????<strong>instance = new Singleton();  //????????
????}
????}
????}
????return instance;
????}
????}
?????????????θ?????instance= new Singleton()??????????????????????????????????????????????漸??JVM???
????memory =allocate();    //1???????????????
????ctorInstance(memory);  //2???????????
????instance =memory;     //3??????instance?????????????
???????????2?????????1?????????3?????????????2??????JVM????????????????????????????????????????????£?
????memory =allocate();    //1???????????????
????instance =memory;     //3??instance??????????????????????δ?????
????ctorInstance(memory);  //2???????????
?????????????????????instance??????????????????棬???????????????????????檔
?????????A?????θ??????????????????????????丳???instance??????????????????????ж?instance???ò??null????????????????3????