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 3
3
// Initialization styles - Pages 333-
4
4
public class Initialization {
5
5
6
- // Normal initialization of an instance field - Page 282
6
+ // Normal initialization of an instance field4 - Page 282
7
7
private final FieldType field1 = computeFieldValue ();
8
8
9
- // Lazy initialization of instance field - synchronized accessor - Page 333
9
+ // Lazy initialization of instance field4 - synchronized accessor - Page 333
10
10
private FieldType field2 ;
11
11
private synchronized FieldType getField2 () {
12
12
if (field2 == null )
@@ -27,15 +27,17 @@ private static class FieldHolder {
27
27
28
28
private FieldType getField4 () {
29
29
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 ;
35
37
}
36
- return result ;
37
38
}
38
39
40
+
39
41
// Single-check idiom - can cause repeated initialization! - Page 334
40
42
private volatile FieldType field5 ;
41
43
Original file line number Diff line number Diff line change 17
17
@ Retention (RetentionPolicy .RUNTIME )
18
18
@ Target (ElementType .METHOD )
19
19
public @interface ExceptionTest {
20
- Class <? extends Exception >[] value ();
20
+ Class <? extends Throwable >[] value ();
21
21
}
Original file line number Diff line number Diff line change @@ -29,9 +29,9 @@ public static void main(String[] args) throws Exception {
29
29
} catch (Throwable wrappedExc ) {
30
30
Throwable exc = wrappedExc .getCause ();
31
31
int oldPassed = passed ;
32
- Class <? extends Exception >[] excTypes =
32
+ Class <? extends Throwable >[] excTypes =
33
33
m .getAnnotation (ExceptionTest .class ).value ();
34
- for (Class <? extends Exception > excType : excTypes ) {
34
+ for (Class <? extends Throwable > excType : excTypes ) {
35
35
if (excType .isInstance (exc )) {
36
36
passed ++;
37
37
break ;
Original file line number Diff line number Diff line change 12
12
@ Target (ElementType .METHOD )
13
13
@ Repeatable (ExceptionTestContainer .class )
14
14
public @interface ExceptionTest {
15
- Class <? extends Exception > value ();
15
+ Class <? extends Throwable > value ();
16
16
}
You can’t perform that action at this time.
0 commit comments