File tree Expand file tree Collapse file tree 1 file changed +8
-7
lines changed
singleton/src/main/java/com/iluwatar/singleton Expand file tree Collapse file tree 1 file changed +8
-7
lines changed Original file line number Diff line number Diff line change 2727 * <p>Thread-safe Singleton class. The instance is lazily initialized and thus needs synchronization
2828 * mechanism.</p>
2929 *
30- * <p>Note: if created by reflection then a singleton will not be created but multiple options
31- * in the same classloader</p>
3230 */
3331public final class ThreadSafeLazyLoadedIvoryTower {
3432
35- private static ThreadSafeLazyLoadedIvoryTower instance ;
33+ private static volatile ThreadSafeLazyLoadedIvoryTower instance ;
3634
3735 private ThreadSafeLazyLoadedIvoryTower () {
38- // protect against instantiation via reflection
36+ // Protect against instantiation via reflection
3937 if (instance == null ) {
4038 instance = this ;
4139 } else {
@@ -44,13 +42,16 @@ private ThreadSafeLazyLoadedIvoryTower() {
4442 }
4543
4644 /**
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
4846 */
4947 public static synchronized ThreadSafeLazyLoadedIvoryTower getInstance () {
5048 if (instance == null ) {
51- instance = new ThreadSafeLazyLoadedIvoryTower ();
49+ synchronized (ThreadSafeLazyLoadedIvoryTower .class ) {
50+ if (instance == null ) {
51+ instance = new ThreadSafeLazyLoadedIvoryTower ();
52+ }
53+ }
5254 }
53-
5455 return instance ;
5556 }
5657}
You can’t perform that action at this time.
0 commit comments