File tree Expand file tree Collapse file tree 3 files changed +86
-0
lines changed
singleton/src/main/java/com/iluwatar Expand file tree Collapse file tree 3 files changed +86
-0
lines changed Original file line number Diff line number Diff line change @@ -31,5 +31,15 @@ public static void main(String[] args) {
31
31
EnumIvoryTower enumIvoryTower2 = EnumIvoryTower .INSTANCE ;
32
32
System .out .println ("enumIvoryTower1=" + enumIvoryTower1 );
33
33
System .out .println ("enumIvoryTower2=" + enumIvoryTower2 );
34
+
35
+ InitializingOnDemandHolderIdiom demandHolderIdiom = InitializingOnDemandHolderIdiom .getInstance ();
36
+ System .out .println (demandHolderIdiom );
37
+ InitializingOnDemandHolderIdiom demandHolderIdiom2 = InitializingOnDemandHolderIdiom .getInstance ();
38
+ System .out .println (demandHolderIdiom2 );
39
+
40
+ ThreadSafeDoubleCheckLocking dcl1 = ThreadSafeDoubleCheckLocking .getInstance ();
41
+ System .out .println (dcl1 );
42
+ ThreadSafeDoubleCheckLocking dcl2 = ThreadSafeDoubleCheckLocking .getInstance ();
43
+ System .out .println (dcl2 );
34
44
}
35
45
}
Original file line number Diff line number Diff line change
1
+ package com .iluwatar ;
2
+
3
+ import java .io .Serializable ;
4
+
5
+ /**
6
+ * The Initialize-on-demand-holder idiom is a secure way of
7
+ * creating lazy initialize singleton Object in Java.
8
+ * refer to "The CERT Oracle Secure Coding Standard for Java"
9
+ * By Dhruv Mohindra, Robert C. Seacord p.378
10
+ *
11
+ * Singleton objects usually are heavy to create and sometimes need to serialize them.
12
+ * This class also shows how to preserve singleton in Serialized version of singleton.
13
+ *
14
+ * @author mortezaadi@gmail.com
15
+ *
16
+ */
17
+ public class InitializingOnDemandHolderIdiom implements Serializable {
18
+
19
+ private static final long serialVersionUID = 1L ;
20
+
21
+ private static class HelperHolder {
22
+ public static final InitializingOnDemandHolderIdiom INSTANCE = new InitializingOnDemandHolderIdiom ();
23
+ }
24
+
25
+ public static InitializingOnDemandHolderIdiom getInstance () {
26
+ return HelperHolder .INSTANCE ;
27
+ }
28
+
29
+ private InitializingOnDemandHolderIdiom () {
30
+ }
31
+
32
+ protected Object readResolve () {
33
+ return getInstance ();
34
+ }
35
+
36
+ }
Original file line number Diff line number Diff line change
1
+ package com .iluwatar ;
2
+
3
+ /**
4
+ * Double check locking
5
+ *
6
+ * http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
7
+ *
8
+ * Broken under Java 1.4.
9
+ * @author mortezaadi@gmail.com
10
+ *
11
+ */
12
+ public class ThreadSafeDoubleCheckLocking {
13
+
14
+ private static volatile ThreadSafeDoubleCheckLocking INSTANCE ;
15
+
16
+ /**
17
+ * private constructor to prevent client from instantiating.
18
+ *
19
+ */
20
+ private ThreadSafeDoubleCheckLocking () {
21
+ //to prevent instantiating by Reflection call
22
+ if (INSTANCE != null )
23
+ throw new IllegalStateException ("Already initialized." );
24
+ }
25
+
26
+ public static ThreadSafeDoubleCheckLocking getInstance () {
27
+ //local variable increases performance by 25 percent
28
+ //Joshua Bloch "Effective Java, Second Edition", p. 283-284
29
+ ThreadSafeDoubleCheckLocking result = INSTANCE ;
30
+ if (result == null ) {
31
+ synchronized (ThreadSafeDoubleCheckLocking .class ) {
32
+ result = INSTANCE ;
33
+ if (result == null ) {
34
+ INSTANCE = result = new ThreadSafeDoubleCheckLocking ();
35
+ }
36
+ }
37
+ }
38
+ return result ;
39
+ }
40
+ }
You can’t perform that action at this time.
0 commit comments