HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
📝
남득윤 학습 저장소
/
객체 지향 프로그래밍
객체 지향 프로그래밍
/
디자인 패턴
디자인 패턴
/
1️⃣
Singleton Pattern
/
😀
Thread-safe implementation
😀

Thread-safe implementation

🤔
인스턴스 제공 멤버로 필드 vs 메서드

  • Early instantiation using implementation with static field
//Early instantiation using implementation with static field. class Singleton { private static final Singleton instance = new Singleton(); private Singleton(){} public static synchronized Singleton getInstance(){ return instance; } }
  • Lazy instantiation using double chekced locking mechanism.
//Lazy instantiation using double chekced locking mechanism. class Singleton { private static volatile Singleton instance; //자바 1.4이하의 메모리 관리 기술도 이해해야함 private Singleton(){ } public static Singleton getInstance(){ if (instance == null) { synchronized(Singleton.class) { if (instance == null) { instance = new Singleton(); } } } return instance; } }