java單例全解析

來源:互聯網
上載者:User

總結了兩個經典的懶載入單例模式

先上代碼:

[java]  view plain  copy public class ImageLoader {          private volatile static ImageLoader instance;          // Returns singleton class instance       public static ImageLoader getInstance() {           if (instance == null) {               synchronized (ImageLoader.class) {                   if (instance == null) {                       instance = new ImageLoader();                   }               }           }           return instance;       }   }  

這是EventBus中的代碼。

如果直接給方法加上synchronized。所有getInstance()的調用都要同步了。其實我們只是在第一次new對象的時候要同步。而同步需要消耗效能


因此兩次檢查是否instance == null,第一個null是為了避免每次都加鎖,畢竟這有一定的開銷。第二個是為了避免同步問題,比如十個線程同時調用getInstance()方法,都執行了第一步檢查instance == null,並且其中一個線程成功獲得了鎖(執行了synchronized語句),接下來如果沒有再一次判斷instance == null,則十個線程將產生10對象,這違背了單例的初衷。

上述代碼除了有兩次檢查instance == null,另一個特點是變數instance的型別宣告中添加了volatile,因為像下面這種建立對象的語句並不是原子操作,volatile可以使其成為原子操作,避免同步問題。 [java]  view plain  copy   instance = new ImageLoader();  

雖說部分JVM沒有完全volatile,但是目前主流的JVM貌似已經都支援了,所以這個問題一般可以忽略。



另外推薦一個更好的簡潔方法:

使用靜態內部類。

public class OssClient {    //log    private  Logger logger = LoggerFactory.getLogger(this.getClass());    //阿里雲API的內或外網網域名稱    private static String END_POINT;    //阿里雲API的密鑰Access Key ID    private static String ACCESS_KEY_ID;    //阿里雲API的密鑰Access Key Secret    private static String ACCESS_KEY_SECRET;    //阿里雲API的BUCKET_NAME    private static String BUCKET_NAME;    public static class clientSingletonHolder{        //init on demand        static  OSSClient client = new OSSClient(END_POINT,ACCESS_KEY_ID, ACCESS_KEY_SECRET);    }    /**     * 擷取阿里雲OSS用戶端對象     * */    public OSSClient getOSSClient(){        return clientSingletonHolder.client;    }
}


解釋一下,因為java機制規定,內部類clientSingletonHolder只有在getOSSClient()方法第一次調用的時候才會被載入(實現了lazy),而且其載入過程是安全執行緒的(實現安全執行緒)。內部類載入的時候執行個體化一次client 。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.