-
Notifications
You must be signed in to change notification settings - Fork 0
5주차: 싱글톤 패턴 #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
5주차: 싱글톤 패턴 #5
Conversation
| /** | ||
| * lazy initialization -> 동기화 문제 | ||
| */ | ||
| public class ChocolateBoilerLazy { | ||
| private boolean empty; | ||
| private boolean boiled; | ||
|
|
||
| private static ChocolateBoilerLazy uniqueInstance; | ||
|
|
||
| private ChocolateBoilerLazy() { | ||
| empty = true; | ||
| boiled = false; // 보일러가 비어있을 때만 돌아간다 | ||
| } | ||
|
|
||
| public static ChocolateBoilerLazy getInstance() { | ||
| if (uniqueInstance == null) { // 멀티 스레딩 환경에서 최초로 인스턴스가 생성될 시점에 동기화 문제 발생 | ||
| uniqueInstance = new ChocolateBoilerLazy(); | ||
| } | ||
|
|
||
| return uniqueInstance; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 멀티 스레드 환경에서 동기화 문제가 있는 고전적 방식의 싱글톤 패턴
| /** | ||
| * lazy initialization & synchronized | ||
| */ | ||
| public class ChocolateBoilerSynchronized { | ||
| private boolean empty; | ||
| private boolean boiled; | ||
|
|
||
| private static ChocolateBoilerSynchronized uniqueInstance; | ||
|
|
||
| private ChocolateBoilerSynchronized() { | ||
| empty = true; | ||
| boiled = false; // 보일러가 비어있을 때만 돌아간다 | ||
| } | ||
|
|
||
| // 인스턴스가 최초로 초기화 되는 시점에만 동기화가 필요하지만, | ||
| // 이 방식은 항상 동기화가 일어나 속도 문제가 있다. | ||
| public static synchronized ChocolateBoilerSynchronized getInstance() { | ||
| if (uniqueInstance == null) { | ||
| uniqueInstance = new ChocolateBoilerSynchronized(); | ||
| } | ||
|
|
||
| return uniqueInstance; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- synchronized 키워드를 활용하여 동기화 문제를 해결한 방식
하지만 속도 문제가 존재한다.
|
|
||
| /** | ||
| * lazy initialization & volatile | ||
| */ | ||
| public class ChocolateBoilerVolatile { | ||
| private boolean empty; | ||
| private boolean boiled; | ||
|
|
||
| private volatile static ChocolateBoilerVolatile uniqueInstance; | ||
|
|
||
| private ChocolateBoilerVolatile() { | ||
| empty = true; | ||
| boiled = false; // 보일러가 비어있을 때만 돌아간다 | ||
| } | ||
|
|
||
| public static ChocolateBoilerVolatile getInstance() { | ||
| if (uniqueInstance == null) { | ||
| // 인스턴스가 최초로 초기화되는 시점에만 동기화가 일어난다. | ||
| // 다만 java 5 버전 이하의 JVM에서는 volatile 키워드가 제대로 동작하지 않는 이슈가 있다. | ||
| synchronized (ChocolateBoilerVolatile.class) { | ||
| if (uniqueInstance == null) { | ||
| uniqueInstance = new ChocolateBoilerVolatile(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return uniqueInstance; | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- synchroized 방식의 속도 문제를 해결하기 위해 volatile 키워드를 활용
다만 java5 버전 이하에서는 volatile 키워드가 제대로 동작하지 않는 이슈가 있다
| /** | ||
| * public static final field | ||
| */ | ||
| public class ChocolateBoilerFinal { | ||
| private boolean empty; | ||
| private boolean boiled; | ||
|
|
||
| // 싱글톤 인스턴스를 처음부터 초기화한다. | ||
| private static final ChocolateBoilerFinal uniqueInstance = new ChocolateBoilerFinal(); | ||
|
|
||
| private ChocolateBoilerFinal() { | ||
| empty = true; | ||
| boiled = false; // 보일러가 비어있을 때만 돌아간다 | ||
| } | ||
|
|
||
| public static ChocolateBoilerFinal getInstance() { | ||
| return uniqueInstance; | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 게으른 인스턴스 생성 방식대신, 처음부터 싱글톤 패턴을 초기화하는 방식
| /** | ||
| * enum으로 싱글톤을 생성하면 | ||
| * 동기화 문제, 클래스 로딩 문제, 리플렉션, 직렬화, 역직렬화 문제를 해결할 수 있다. | ||
| */ | ||
| public enum ChocolateBoilerEnum { | ||
| UNIQUE_INSTANCE; | ||
|
|
||
| private boolean empty; | ||
| private boolean boiled; | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 현재 가장 완벽한 enum 클래스를 활용한 싱글톤 방식
동기화 문제, 클래스 로딩 문제, 리플렉션, 직렬화, 역직렬화 문제를 해결할 수 있다.
src/singleton/싱글톤 패턴.md
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.
No description provided.