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單例模式中的缺陷及單例模式的其他實現單例模式
- java單例模式懶漢和餓漢Java單例模式
- 詳談單例、餓漢、和懶漢模式單例模式
- 單張圖片懶載入
- Volatile關鍵字&&DCL單例模式,volatile 和 synchronized 的區別單例模式synchronized
- 別再用懶漢模式了——從JVM的角度看單例模式模式JVM單例
- 懶載入
- 設計模式:單例模式 (關於餓漢式和懶漢式)設計模式單例
- Spring原始碼剖析4:懶載入的單例Bean獲取過程分析Spring原始碼單例Bean
- 懶載入和預載入
- Ribbon - 懶載入
- java設計模式之單例模式你真的會了嗎?(懶漢式篇)Java設計模式單例
- 舉例說明圖片懶載入的方案有哪些?
- 【譯】懶載入元件元件
- 圖片懶載入
- vue路由懶載入Vue路由
- Vue元件懶載入Vue元件
- Hibernate 之 懶載入
- Vue 的懶載入Vue
- 【java】單例設計模式——餓漢式和懶漢式、Runtime、TimerJava單例設計模式
- 圖片預載入和懶載入
- Vue 路由按需載入(路由懶載入)Vue路由
- JavaScript面試系列:JavaScript設計模式之橋接模式和懶載入JavaScript面試設計模式橋接
- scala:分別使用懶漢式和餓漢式實現單例模式單例模式
- 前端效能優化 --- 懶載入&預載入前端優化
- 圖片懶載入(IntersectionObserver)Server
- 關於懶載入原理
- Fragment 懶載入實踐Fragment
- 懶載入之intersection observerServer
- 圖片懶載入原理
- vue(18)路由懶載入Vue路由
- 單例模式單例模式
- 手把手實現圖片懶載入+封裝vue懶載入元件封裝Vue元件
- 多執行緒下指令重排與DCL單列模式執行緒模式
- [譯] React 16.6 懶載入(與預載入)元件React元件
- 圖片懶載入踩坑
- 圖片懶載入大白話