DCL懶載入單例模式
public class TomcatURLStreamHandlerFactory implements URLStreamHandlerFactory {
// Singleton instance
private static volatile TomcatURLStreamHandlerFactory instance = null;
/**
* Obtain a reference to the singleton instance. It is recommended that
* callers check the value of {@link #isRegistered()} before using the
* returned instance.
*
* @return A reference to the singleton instance
*/
public static TomcatURLStreamHandlerFactory getInstance() {
getInstanceInternal(true);
return instance;
}
private static TomcatURLStreamHandlerFactory getInstanceInternal(boolean register) {
// Double checked locking. OK because instance is volatile.
if (instance == null) {
synchronized (TomcatURLStreamHandlerFactory.class) {
if (instance == null) {
instance = new TomcatURLStreamHandlerFactory(register);
}
}
}
return instance;
}
}
又比如
public class DefaultConversionService extends GenericConversionService{
private static volatile DefaultConversionService sharedInstance;
/**
* Return a shared default {@code ConversionService} instance,
* lazily building it once needed.
* <p><b>NOTE:</b> We highly recommend constructing individual
* {@code ConversionService} instances for customization purposes.
* This accessor is only meant as a fallback for code paths which
* need simple type coercion but cannot access a longer-lived
* {@code ConversionService} instance any other way.
* @return the shared {@code ConversionService} instance (never {@code null})
* @since 4.3.5
*/
public static ConversionService getSharedInstance() {
if (sharedInstance == null) {
synchronized (DefaultConversionService.class) {
if (sharedInstance == null) {
sharedInstance = new DefaultConversionService();
}
}
}
return sharedInstance;
}
}
相關文章
- DCL之單例模式單例模式
- DCL單例模式中的缺陷及單例模式的其他實現單例模式
- 懶載入例項--hibernate
- 單張圖片懶載入
- java單例模式懶漢和餓漢Java單例模式
- 懶載入簡單的方法
- 詳談單例、餓漢、和懶漢模式單例模式
- 懶載入
- 設計模式——懶漢式單例類PK餓漢式單例類設計模式單例
- 別再用懶漢模式了——從JVM的角度看單例模式模式JVM單例
- 設計模式-單例模式之懶漢式-多執行緒設計模式單例執行緒
- 懶載入和預載入
- iOS: 懶載入iOS
- Volatile關鍵字&&DCL單例模式,volatile 和 synchronized 的區別單例模式synchronized
- 設計模式:單例模式 (關於餓漢式和懶漢式)設計模式單例
- Spring原始碼剖析4:懶載入的單例Bean獲取過程分析Spring原始碼單例Bean
- 【譯】懶載入元件元件
- 圖片懶載入
- Vue元件懶載入Vue元件
- Ribbon - 懶載入
- vue路由懶載入Vue路由
- Vue 的懶載入Vue
- Swift中懶載入Swift
- hibernate懶載入
- spring 懶載入Spring
- 什麼是hibernate懶載入?什麼時候用懶載入?為什麼要用懶載入?(轉)
- Vue 路由按需載入(路由懶載入)Vue路由
- 圖片預載入和懶載入
- JavaScript面試系列:JavaScript設計模式之橋接模式和懶載入JavaScript面試設計模式橋接
- 【Spring原始碼分析】非懶載入的單例Bean初始化過程(下篇)Spring原始碼單例Bean
- 【Spring原始碼分析】非懶載入的單例Bean初始化過程(上篇)Spring原始碼單例Bean
- java設計模式之單例模式你真的會了嗎?(懶漢式篇)Java設計模式單例
- 前端效能優化 --- 懶載入&預載入前端優化
- 圖片懶載入(IntersectionObserver)Server
- vue(18)路由懶載入Vue路由
- 關於懶載入原理
- 懶載入之intersection observerServer
- 圖片懶載入原理