2014-09-24 2 views

답변

3

인스턴스로 사용 된 실제 클래스는 이 아니고 threadsafe (작성하지 않은 경우)입니다. 주변 사례가 많이 있습니다 (예를 들어 Are final static variables thread safe in Java? : 스레드하지 않다가 사용되는 정적 최종 HashMap)

생성 groovys을 @Singleton 주석 스레드 (그리고 당신이에 의존한다이다 사용하여 싱글의). 여기

  1. 다시 자바 스레드 인 static final 변수 결과 일반 버전 @Singleton 인 :

    docs은 변환에 의해 생성되는 해당 자바 코드의 두 개의 버전을 표시

    lazy 버전에 대한
    public class T { 
        public static final T instance = new T(); 
        private T() {} 
    } 
    
  2. (@Singleton(lazy=true)) Double-checked locking이 만들어집니다 : 참고로

    class T { 
        private static volatile T instance 
        private T() {} 
        static T getInstance() { 
         if (instance) { 
          instance 
         } else { 
          synchronized(T) { 
           if (instance) { 
            instance 
           } else { 
            instance = new T() 
           } 
          } 
         } 
        } 
    } 
    

이 여기에 당신이 당신의 클래스 스레드의 모든 방법이 안전하게 않는 의미 gist with a simple class and the disassembled code