사용자 도구

사이트 도구


java:lazy_evaluation

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
마지막 판 양쪽 다음 판
java:lazy_evaluation [2020/05/18 17:41]
kwon37xi
java:lazy_evaluation [2020/05/18 17:42]
kwon37xi [Effective Java 3rd Item 83 - double check]
줄 9: 줄 9:
   * [[https://github.com/jbloch/effective-java-3e-source-code/issues/8|Double-check idiom for lazy initialization of instance fields. Broken example. · Issue #8 · jbloch/effective-java-3e-source-code]]   * [[https://github.com/jbloch/effective-java-3e-source-code/issues/8|Double-check idiom for lazy initialization of instance fields. Broken example. · Issue #8 · jbloch/effective-java-3e-source-code]]
 <code java> <code java>
-    // Double-check idiom for lazy initialization of instance fields - Page 334 +// Double-check idiom for lazy initialization of instance fields - Page 334 
-    private volatile FieldType field4;+private volatile FieldType field4;
  
-    // NOTE: The code for this method in the first printing had a serious error (see errata for details)! +// NOTE: The code for this method in the first printing had a serious error (see errata for details)! 
-    private FieldType getField4() { +private FieldType getField4() { 
-        FieldType result = field4; +    FieldType result = field4; 
-        if (result != null) // First check (no locking) +    if (result != null) // First check (no locking) 
-            return result;+        return result;
  
-        synchronized(this) { +    synchronized(this) { 
-            if (field4 == null) // Second check (with locking) +        if (field4 == null) // Second check (with locking) 
-                field4 = computeFieldValue(); +            field4 = computeFieldValue(); 
-            return field4; +        return field4;
-        }+
     }     }
 +}
 </code> </code>
java/lazy_evaluation.txt · 마지막으로 수정됨: 2020/05/19 09:28 저자 kwon37xi