안녕하세요
문득. 아이러니한게. 보통 싱글톤 클래스의 인스턴스는
static final이 잖아요? 그럼 아무리 멀티스레드 환경에서 인스턴스가 두 개 이상 생성되었을지 언정.
결국엔 싱글돈 클레스의 static 인스턴스는 어떤 순서로 생성이 됐건. 최종적으로 하나의 인스턴스만 생성이 되기 때문에.
비록 초기에 낮은 확률로 두 개의 인스턴스가 생성되는 문제가 있지만.. 결국엔 하나의 인스턴스만 갖는다는 원칙을 준수하기 때문에 별다른 문제가 없는거 아닌가요?
스텍오버플로 질문도 해봤는데. 싱글톤 인스턴스가 원식 값 즉, int, long, double 이라면 문제가 없다.
단, list와 같은 인스턴스이면 static final 이 더라도 상호 다른 객체를 바라본다는데. 이해가가질 않습니다.
---- 참고 -----
Q) static instance may be 'final', Isn't it okay to have a multi-threaded environment?
Anyway, eventually, an instance is created.
Is there any other problem that I didn't think of?
A) If your static final field is an int then yes, all threads will always read the same value. If your static final field is a List then no, threads may see different contents even if the list is static final. So it really depends on what the object is. --> 이게 가능한 일인지? 이해가 가질 않습니다.