File tree 3 files changed +32
-20
lines changed
singleton/src/main/java/com/iluwatar
3 files changed +32
-20
lines changed Original file line number Diff line number Diff line change 1
1
package com .iluwatar ;
2
2
3
3
/**
4
- *
4
+ *
5
5
* Singleton pattern ensures that the class (IvoryTower) can have only one
6
6
* existing instance and provides global access to that instance.
7
- *
7
+ *
8
8
*/
9
9
public class App {
10
10
@@ -15,5 +15,12 @@ public static void main(String[] args) {
15
15
System .out .println ("ivoryTower1=" + ivoryTower1 );
16
16
System .out .println ("ivoryTower2=" + ivoryTower2 );
17
17
18
+ ThreadSafeLazyLoadedIvoryTower threadSafeIvoryTower1 = ThreadSafeLazyLoadedIvoryTower
19
+ .getInstance ();
20
+ ThreadSafeLazyLoadedIvoryTower threadSafeIvoryTower2 = ThreadSafeLazyLoadedIvoryTower
21
+ .getInstance ();
22
+ System .out .println ("threadSafeIvoryTower1=" + threadSafeIvoryTower1 );
23
+ System .out .println ("threadSafeIvoryTower2=" + threadSafeIvoryTower2 );
24
+
18
25
}
19
26
}
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ package com .iluwatar ;
2
+
3
+ /**
4
+ *
5
+ * Thread-safe Singleton class.
6
+ *
7
+ */
8
+ public class ThreadSafeLazyLoadedIvoryTower {
9
+
10
+ private static ThreadSafeLazyLoadedIvoryTower instance = null ;
11
+
12
+ public synchronized static ThreadSafeLazyLoadedIvoryTower getInstance () {
13
+ /*
14
+ * The instance gets created only when it is called for first time.
15
+ * Lazy-loading
16
+ */
17
+ if (instance == null ) {
18
+ instance = new ThreadSafeLazyLoadedIvoryTower ();
19
+ }
20
+
21
+ return instance ;
22
+ }
23
+ }
You can’t perform that action at this time.
0 commit comments