File tree Expand file tree Collapse file tree 4 files changed +14
-12
lines changed
Expand file tree Collapse file tree 4 files changed +14
-12
lines changed Original file line number Diff line number Diff line change 33// Initialization styles - Pages 333-
44public class Initialization {
55
6- // Normal initialization of an instance field - Page 282
6+ // Normal initialization of an instance field4 - Page 282
77 private final FieldType field1 = computeFieldValue ();
88
9- // Lazy initialization of instance field - synchronized accessor - Page 333
9+ // Lazy initialization of instance field4 - synchronized accessor - Page 333
1010 private FieldType field2 ;
1111 private synchronized FieldType getField2 () {
1212 if (field2 == null )
@@ -27,15 +27,17 @@ private static class FieldHolder {
2727
2828 private FieldType getField4 () {
2929 FieldType result = field4 ;
30- if (result == null ) { // First check (no locking)
31- synchronized (this ) {
32- if (field4 == null ) // Second check (with locking)
33- field4 = result = computeFieldValue ();
34- }
30+ if (result != null ) // First check (no locking)
31+ return result ;
32+
33+ synchronized (this ) {
34+ if (field4 == null ) // Second check (with locking)
35+ field4 = computeFieldValue ();
36+ return field4 ;
3537 }
36- return result ;
3738 }
3839
40+
3941 // Single-check idiom - can cause repeated initialization! - Page 334
4042 private volatile FieldType field5 ;
4143
Original file line number Diff line number Diff line change 1717@ Retention (RetentionPolicy .RUNTIME )
1818@ Target (ElementType .METHOD )
1919public @interface ExceptionTest {
20- Class <? extends Exception >[] value ();
20+ Class <? extends Throwable >[] value ();
2121}
Original file line number Diff line number Diff line change @@ -29,9 +29,9 @@ public static void main(String[] args) throws Exception {
2929 } catch (Throwable wrappedExc ) {
3030 Throwable exc = wrappedExc .getCause ();
3131 int oldPassed = passed ;
32- Class <? extends Exception >[] excTypes =
32+ Class <? extends Throwable >[] excTypes =
3333 m .getAnnotation (ExceptionTest .class ).value ();
34- for (Class <? extends Exception > excType : excTypes ) {
34+ for (Class <? extends Throwable > excType : excTypes ) {
3535 if (excType .isInstance (exc )) {
3636 passed ++;
3737 break ;
Original file line number Diff line number Diff line change 1212@ Target (ElementType .METHOD )
1313@ Repeatable (ExceptionTestContainer .class )
1414public @interface ExceptionTest {
15- Class <? extends Exception > value ();
15+ Class <? extends Throwable > value ();
1616}
You can’t perform that action at this time.
0 commit comments