File tree 1 file changed +8
-7
lines changed
singleton/src/main/java/com/iluwatar/singleton
1 file changed +8
-7
lines changed Original file line number Diff line number Diff line change 27
27
* <p>Thread-safe Singleton class. The instance is lazily initialized and thus needs synchronization
28
28
* mechanism.</p>
29
29
*
30
- * <p>Note: if created by reflection then a singleton will not be created but multiple options
31
- * in the same classloader</p>
32
30
*/
33
31
public final class ThreadSafeLazyLoadedIvoryTower {
34
32
35
- private static ThreadSafeLazyLoadedIvoryTower instance ;
33
+ private static volatile ThreadSafeLazyLoadedIvoryTower instance ;
36
34
37
35
private ThreadSafeLazyLoadedIvoryTower () {
38
- // protect against instantiation via reflection
36
+ // Protect against instantiation via reflection
39
37
if (instance == null ) {
40
38
instance = this ;
41
39
} else {
@@ -44,13 +42,16 @@ private ThreadSafeLazyLoadedIvoryTower() {
44
42
}
45
43
46
44
/**
47
- * The instance gets created only when it is called for first time. Lazy-loading
45
+ * The instance doesn't get created until the method is called for the first time
48
46
*/
49
47
public static synchronized ThreadSafeLazyLoadedIvoryTower getInstance () {
50
48
if (instance == null ) {
51
- instance = new ThreadSafeLazyLoadedIvoryTower ();
49
+ synchronized (ThreadSafeLazyLoadedIvoryTower .class ) {
50
+ if (instance == null ) {
51
+ instance = new ThreadSafeLazyLoadedIvoryTower ();
52
+ }
53
+ }
52
54
}
53
-
54
55
return instance ;
55
56
}
56
57
}
You can’t perform that action at this time.
0 commit comments