Skip to content
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