基于Java利用static實現(xiàn)單例模式
一、之前舊的寫法
class Singleton{ ? ? private Singleton() {} ? ? private static Singleton instance = null; ? ? public synchronized static Singleton getInstance() { ? ? ? ? ? ? if (instance == null) { ? ? ? ? ? ? ? ? instance = new Singleton(); ? ? ? ? ? ? } ? ? ? ? ? ? return instance; ? ? ? ? } }
就利用Sington.getInstace
就可以了,獲得的是同一個實例。
上面那個代碼有兩個優(yōu)點:
- 懶加載,把在堆創(chuàng)建實例這個行為延遲到類的使用時。
- 鎖效果,防止生成多個實例,因為
synchronized
修飾這個static
方法,所以相當于給這個方法加上了一個類鎖??。
二、static代碼塊的效果
先來看一段代碼:
class StaticClass{ ? ? private static int a = 1; ? ? static{ ? ? ? ? System.out.println("語句1"); ? ? ? ? System.out.println("語句2"); ? ? } ? ? static{ ? ? ? ? System.out.println("語句3"); ? ? ? ? System.out.println("語句4"); ? ? } }
當在多個線程同時觸發(fā)類的初始化過程的時候(在初始化過程,類的static變量會被賦值為JVM默認值并且static
代碼塊會被執(zhí)行),為什么static
不會被多次執(zhí)行?因為有可能兩個線程同時檢測到類還沒被初始化,然后都執(zhí)行static代碼塊,結果就把語句1234多打印了,為什么上述情況不會發(fā)生。
Thread thread1 = new Thread(new Runnable() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void run() { ? ? ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? ? ? Class.forName("StaticClass");//這一行觸發(fā)類的初始化導致靜態(tài)代碼塊執(zhí)行 ? ? ? ? ? ? ? ? } catch (ClassNotFoundException e) { ? ? ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? thread1.start(); ? ? ? ? Thread thread2 = new Thread(new Runnable() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void run() { ? ? ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? ? ? Class.forName("StaticClass");//同樣 ? ? ? ? ? ? ? ? } catch (ClassNotFoundException e) { ? ? ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? thread2.start();
結果如下:
語句1
語句2
語句3
語句4
有一段英文對其進行了解釋:
static initialization block can be triggered from multiple parallel threads (when the loading of the class happens in the first time), Java runtime guarantees that it will be executed only once and in thread-safe manner + when we have more than 1 static block - it guarantees the sequential execution of the blocks, 也就是說,java runtime幫我們做了兩件事:
- 在并行線程中,都出現(xiàn)了第一次初始化類的情況,保證類的初始化只執(zhí)行一次。
- 保證
static
代碼塊的順序執(zhí)行
三、單例的另一種寫法
有了對static
的知識的了解之后,我們可以寫出這樣的單例模式:
class Singleton{ ? ? private Singleton() {} ? ? private static class NestedClass{ ? ? ? ?static Singleton instance = new Singleton();//這條賦值語句會在初始化時才運行 ? ? } ? ? public static Singleton getInstance() { ? ? ? ? return NestedClass.instance; ? ? } }
- 懶加載,因為
static
語句會在初始化時才賦值運行,達到了懶加載的效果。 - 鎖??的效果由
Java runtime
保證,虛擬機幫我們保證static語句在初始化時只會執(zhí)行一次。
四、總結
如果不知道static
的基礎知識和虛擬機類加載的知識,我可能并不會知道這一種方法。理論永遠先行于技術,要學好理論才能從根本上提升自己。
到此這篇關于基于Java利用static實現(xiàn)單例模式的文章就介紹到這了,更多相關static實現(xiàn)單例模式內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Springboot項目出現(xiàn)java.lang.ArrayStoreException的異常分析
這篇文章介紹了Springboot項目出現(xiàn)java.lang.ArrayStoreException的異常分析,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-12-12Springboot創(chuàng)建項目的圖文教程(idea版本)
這篇文章主要介紹了Springboot創(chuàng)建項目的圖文教程(idea版本),本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06MyBatisPlus?TypeHandler自定義字段類型轉換Handler
這篇文章主要為大家介紹了MyBatisPlus?TypeHandler自定義字段類型轉換Handler示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08