Skip to content

Conversation

@guswns3371
Copy link
Member

No description provided.

Comment on lines +3 to +23
/**
* 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;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 멀티 스레드 환경에서 동기화 문제가 있는 고전적 방식의 싱글톤 패턴

Comment on lines +3 to +25
/**
* 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;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. synchronized 키워드를 활용하여 동기화 문제를 해결한 방식

하지만 속도 문제가 존재한다.

Comment on lines +2 to +30

/**
* 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;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. synchroized 방식의 속도 문제를 해결하기 위해 volatile 키워드를 활용

다만 java5 버전 이하에서는 volatile 키워드가 제대로 동작하지 않는 이슈가 있다

Comment on lines +3 to +21
/**
* 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;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 게으른 인스턴스 생성 방식대신, 처음부터 싱글톤 패턴을 초기화하는 방식

Comment on lines +3 to +12
/**
* enum으로 싱글톤을 생성하면
* 동기화 문제, 클래스 로딩 문제, 리플렉션, 직렬화, 역직렬화 문제를 해결할 수 있다.
*/
public enum ChocolateBoilerEnum {
UNIQUE_INSTANCE;

private boolean empty;
private boolean boiled;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 현재 가장 완벽한 enum 클래스를 활용한 싱글톤 방식

동기화 문제, 클래스 로딩 문제, 리플렉션, 직렬화, 역직렬화 문제를 해결할 수 있다.

Comment on lines +110 to +151
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

@guswns3371 guswns3371 merged commit da1acb7 into main Nov 26, 2022
@guswns3371 guswns3371 deleted the 5week branch November 26, 2022 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant