Skip to content

Commit

Permalink
Thread-safe Singleton class
Browse files Browse the repository at this point in the history
New Singleton class name has renamed to ThreadSafeLazyLoadedIvoryTower
and called it from App.java
  • Loading branch information
annemsujan committed Oct 14, 2014
1 parent cffe592 commit 5687976
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
11 changes: 9 additions & 2 deletions singleton/src/main/java/com/iluwatar/App.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.iluwatar;

/**
*
*
* Singleton pattern ensures that the class (IvoryTower) can have only one
* existing instance and provides global access to that instance.
*
*
*/
public class App {

Expand All @@ -15,5 +15,12 @@ public static void main(String[] args) {
System.out.println("ivoryTower1=" + ivoryTower1);
System.out.println("ivoryTower2=" + ivoryTower2);

ThreadSafeLazyLoadedIvoryTower threadSafeIvoryTower1 = ThreadSafeLazyLoadedIvoryTower
.getInstance();
ThreadSafeLazyLoadedIvoryTower threadSafeIvoryTower2 = ThreadSafeLazyLoadedIvoryTower
.getInstance();
System.out.println("threadSafeIvoryTower1=" + threadSafeIvoryTower1);
System.out.println("threadSafeIvoryTower2=" + threadSafeIvoryTower2);

}
}
18 changes: 0 additions & 18 deletions singleton/src/main/java/com/iluwatar/SingletonClass.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.iluwatar;

/**
*
* Thread-safe Singleton class.
*
*/
public class ThreadSafeLazyLoadedIvoryTower {

private static ThreadSafeLazyLoadedIvoryTower instance = null;

public synchronized static ThreadSafeLazyLoadedIvoryTower getInstance() {
/*
* The instance gets created only when it is called for first time.
* Lazy-loading
*/
if (instance == null) {
instance = new ThreadSafeLazyLoadedIvoryTower();
}

return instance;
}
}

0 comments on commit 5687976

Please sign in to comment.