Skip to content

아이템 3. private 생성자나 열거 타입으로 싱글턴임을 보증하라 #12

Answered by JoisFe
jinan159 asked this question in 3. 과제
Discussion options

You must be logged in to vote

싱글턴을 만들때 3) Lazy Initialization + Synchronization(지연 초기화 + 동기화) 방식으로 멀티스레딩의 문제를 해결하기 위해 사용한 synchronized로 인한 성능의 문제를 해결하는 방법으로
4) LazyHolder 방식을 사용 하였는데 저도 처음 알게 되었습니다.

3) 문제를 해결하는 방법으로 4) 말고도 알고있는 내용으로는
DCL(Double Checked Locking) 방식에 대해 알고 있습니다.

public class Singleton3 {

    private volatile static Singleton3 uniqueInstance;

    private Singleton3() {}

    public static Singleton3 getInstance() {
        if (uniqueInstance == null) {
            synchronized (Singleton3.class) {
                if (uniqueInstance == null) {
                    uniqueInstance = new Singleton3();
                }
            }
        }
        return uniqueInstance;
    }
}

3) 방식의 문제는 동기화가 꼭 필요한 시점은 …

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by jinan159
Comment options

You must be logged in to vote
1 reply
@Irisation23
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2장 객체 생성과 파괴 이펙티브 자바 2장 (객체 생성과 파괴)
4 participants