您好,登錄后才能下訂單哦!
如何實現Singleton模式,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
設計一個類,我們只能生成該類的一個實例
私有構造方法
靜態方法獲取實例
如果需要考慮內存,使用到時才創建實例對象(餓漢),不使用時就不創建實例(懶漢,懶加載)。
如果需要考慮線程安全,就要確保獲取實例是同步的,避免創建多個實例。
[x] 1. 單線程(懶漢式、餓漢式)
[x] 2. 多線程工作效率不高(加鎖獲取實例的方法)
[x] 3. 加同步鎖前后兩次判斷實例是否已存在
[x] 4. 利用靜態初始化創建實例(推薦,線程安全,會占用一部分內存)
[x] 5. 利用靜態內部類實現按需創建實例(最推薦,線程安全,效率高,聰明的你應該可以明白的)
package cn.jast.java.offer.singleton; /** * 簡單餓漢單例 * */ public class SimpleHungerSingleton { private static SimpleHungerSingleton simpleSingleton; private SimpleHungerSingleton(){ simpleSingleton = new SimpleHungerSingleton(); } public static SimpleHungerSingleton getInstance(){ return simpleSingleton; } }
package cn.jast.java.offer.singleton; /** * 簡單懶漢單例 * */ public class SimpleLazySingleton { private static SimpleLazySingleton simpleSingleton; private SimpleLazySingleton(){ } public static SimpleLazySingleton getInstance(){ if(simpleSingleton == null){ simpleSingleton = new SimpleLazySingleton(); } return simpleSingleton; } }
/** * 測試簡單單例的線程安全 */ public static void testSimpleLazySingleton(){ Set<SimpleLazySingleton> singletonSet = Collections.synchronizedSet(new HashSet<>()); ExecutorService executorService = Executors.newFixedThreadPool(50); for (int i = 0; i < 10; i++) { executorService.submit(()->{ SimpleLazySingleton simpleLazySingleton = SimpleLazySingleton.getInstance(); singletonSet.add(simpleLazySingleton); }); } executorService.shutdown(); while(true){ if(executorService.isShutdown()){ if(singletonSet.size()>1){ System.out.println("簡單單例存在創建多個實例對象,實例如下:"); System.out.println(singletonSet); } break; } } }
輸出:
簡單單例存在創建多個實例對象,實例如下: [cn.jast.java.offer.singleton.SimpleLazySingleton@2b9283d, cn.jast.java.offer.singleton.SimpleLazySingleton@72fba635]
package cn.jast.java.offer.singleton; public class Synchronized1Singleton { private static Synchronized1Singleton instance; private Synchronized1Singleton(){ } /** * 每次獲取對象時都加鎖來確保創建對象 * @return */ public static synchronized Synchronized1Singleton getInstance(){ if(instance == null){ instance = new Synchronized1Singleton(); } return instance; } }
測試:
public static void testSynchronized1Singleton(){ long startTime = System.currentTimeMillis(); Set<Synchronized1Singleton> singletonSet = Collections.synchronizedSet(new HashSet<>()); ExecutorService executorService = Executors.newFixedThreadPool(50); for (int i = 0; i < 10; i++) { executorService.submit(()->{ Synchronized1Singleton singleton = Synchronized1Singleton.getInstance(); singletonSet.add(singleton); }); } executorService.shutdown(); while(true){ if(executorService.isShutdown()){ System.out.println(String.format("執行時間:%s ms",System.currentTimeMillis()-startTime)); if(singletonSet.size()>1){ System.out.println("簡單單例存在創建多個實例對象,實例如下:"); System.out.println(singletonSet); } break; } } }
輸出:
執行時間:72 ms(注:一個樣例)
package cn.jast.java.offer.singleton; public class Synchronized2Singleton { private static Synchronized2Singleton instance; private Synchronized2Singleton(){ } public static Synchronized2Singleton getInstance(){ if(instance == null){ synchronized (Synchronized2Singleton.class){ if(instance==null){ instance = new Synchronized2Singleton(); } } } return instance; } }
package cn.jast.java.offer.singleton; /** * 推薦 */ public class StaticInitializeSingleton { private static StaticInitializeSingleton instance ; static{ instance = new StaticInitializeSingleton(); } private StaticInitializeSingleton(){ } public static StaticInitializeSingleton getInstance(){ return instance; } }
package cn.jast.java.offer.singleton; /** * 推薦 */ public class StaticInnerClassSingleton { private StaticInnerClassSingleton(){ } public static StaticInnerClassSingleton getInstance(){ return Inner.instance; } private static class Inner{ private static final StaticInnerClassSingleton instance = new StaticInnerClassSingleton(); } }
package cn.jast.java.offer.singleton; import java.util.*; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Main { public static void main(String[] args) { // testSimpleLazySingleton(); testSynchronized1Singleton(); // testSynchronized2Singleton(); // testStaticInitializeSingleton(); // testNestedClassSingleton(); } /** * 測試簡單單例的線程安全 */ public static void testSimpleLazySingleton(){ Set<SimpleLazySingleton> singletonSet = Collections.synchronizedSet(new HashSet<>()); ExecutorService executorService = Executors.newFixedThreadPool(50); for (int i = 0; i < 10; i++) { executorService.submit(()->{ SimpleLazySingleton simpleLazySingleton = SimpleLazySingleton.getInstance(); singletonSet.add(simpleLazySingleton); }); } executorService.shutdown(); while(true){ if(executorService.isShutdown()){ if(singletonSet.size()>1){ System.out.println("簡單單例存在創建多個實例對象,實例如下:"); System.out.println(singletonSet); } break; } } } /** * 測試線程安全的單例模式實現 */ public static void testSynchronized1Singleton(){ long startTime = System.currentTimeMillis(); Set<Synchronized1Singleton> singletonSet = Collections.synchronizedSet(new HashSet<>()); ExecutorService executorService = Executors.newFixedThreadPool(50); for (int i = 0; i < 10; i++) { executorService.submit(()->{ Synchronized1Singleton singleton = Synchronized1Singleton.getInstance(); singletonSet.add(singleton); }); } executorService.shutdown(); while(true){ if(executorService.isShutdown()){ System.out.println(String.format("執行時間:%s ms",System.currentTimeMillis()-startTime)); if(singletonSet.size()>1){ System.out.println("簡單單例存在創建多個實例對象,實例如下:"); System.out.println(singletonSet); } break; } } } /** * Synchronized2Singleton 的效率比 Synchronized1Singleton高幾倍甚至幾十倍以上 */ public static void testSynchronized2Singleton(){ long startTime = System.currentTimeMillis(); Set<Synchronized2Singleton> singletonSet = Collections.synchronizedSet(new HashSet<>()); ExecutorService executorService = Executors.newFixedThreadPool(50); for (int i = 0; i < 10; i++) { executorService.submit(()->{ Synchronized2Singleton singleton = Synchronized2Singleton.getInstance(); singletonSet.add(singleton); }); } executorService.shutdown(); while(true){ if(executorService.isShutdown()){ System.out.println(String.format("執行時間:%s ms",System.currentTimeMillis()-startTime)); if(singletonSet.size()>1){ System.out.println("簡單單例存在創建多個實例對象,實例如下:"); System.out.println(singletonSet); } break; } } } /** * */ public static void testStaticInitializeSingleton(){ Set<Synchronized2Singleton> singletonSet = Collections.synchronizedSet(new HashSet<>()); ExecutorService executorService = Executors.newFixedThreadPool(50); for (int i = 0; i < 10; i++) { executorService.submit(()->{ Synchronized2Singleton singleton = Synchronized2Singleton.getInstance(); singletonSet.add(singleton); }); } executorService.shutdown(); while(true){ if(executorService.isShutdown()){ if(singletonSet.size()>1){ System.out.println("簡單單例存在創建多個實例對象,實例如下:"); System.out.println(singletonSet); } break; } } } public static void testNestedClassSingleton(){ Set<StaticInnerClassSingleton> singletonSet = Collections.synchronizedSet(new HashSet<>()); ExecutorService executorService = Executors.newFixedThreadPool(50); for (int i = 0; i < 10; i++) { executorService.submit(()->{ StaticInnerClassSingleton singleton = StaticInnerClassSingleton.getInstance(); singletonSet.add(singleton); }); } executorService.shutdown(); while(true){ if(executorService.isShutdown()){ if(singletonSet.size()>1){ System.out.println("簡單單例存在創建多個實例對象,實例如下:"); System.out.println(singletonSet); } break; } } } }
問:單例模式獲取實例的方法為什么是靜態方法? 答:因為構造方法是私有的,無法通過new創建實例,那只能通過類方法獲取實例。那通過反射是否可以創建實例呢?
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。