###做用html
###實現機制 彙編多出一個lock前綴指令(內存柵欄)緩存
###內存柵欄的做用線程
###使用volatile關鍵字的場景code
狀態標記 volatile boolean flag = false; while(!flag){ doSomething(); } public void setFlag() { flag = true; } 禁止重排序 volatile boolean inited = false; //線程1: context = loadContext(); inited = true; //線程2: while(!inited ){ sleep() } doSomethingwithconfig(context); //double check class Singleton{ private volatile static Singleton instance = null; private Singleton() { } public static Singleton getInstance() { if(instance==null) { synchronized (Singleton.class) { if(instance==null) instance = new Singleton(); } } return instance; } }