HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
📝
남득윤 학습 저장소
/
객체 지향 프로그래밍
객체 지향 프로그래밍
/
디자인 패턴
디자인 패턴
/
1️⃣
Singleton Pattern
/
‼️
네 번째 방법은 static inner 클래스 사용하기 (Bill Pugh Solution)
‼️

네 번째 방법은 static inner 클래스 사용하기 (Bill Pugh Solution)

👍
Best Practice

//inner static class 활용 class Singleton { private Singleton() {} private static class SingletonHolder { private static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return SingletonHolder.Instance; } }
 
 
Bill Pugh 솔루션이 Thread Safe 한 이유
 
get Instance를 호출해야 내부 Holder 클래스가 처음 접근 되고 런타임에 동적으로 로딩됨
 
Class를 로딩하고 초기화하는 시점은 thread-safe를 보장함
→ 클래스 로딩이 Single Thread 인 것은 아님 ClassLoader 내부의 Parallel Loader 클래스를 사용 멀티 쓰레드 안정성을 보장