두 인스턴스가 동시에 getInstance에 액세스 할 수 있고 _instance가 null이고 Singleton 클래스의 두 인스턴스를 만드는 경우 새 인스턴스 생성을 동기화하는 이유를 읽습니다. 그래서 그것을 제거하기 위해 우리는 그것을 동기화했습니다. 그러나 다시 한번 이중 검사 잠금 방법이 인스턴스가 Synchronized 블록 내부에서 null인지 다시 확인하는 방법을 다시 설명합니다. 필요한가? 코드 블록이 동기화되면 이미 스레드 안전을 알고 있습니까?스레드 안전이있는 싱글 톤 디자인 패턴
public class Singleton {
private static Singleton _instance = null;
private Singleton() { }
public static Singleton getInstance() {
if (_instance == null) {
synchronized (Singleton.class) {
_instance = new Singleton(); // why need to again check null
}
}
return _instance;
}
}
가능한 복제본 [Java의 지연 스레드 안전 싱글 톤 인스턴스 생성 패턴] (http://stackoverflow.com/questions/3635396/pattern-for-lazy-thread-safe-singleton-instantiation-in-java) – user1937198