Skip to content

Commit 4bf2e3f

Browse files
committed
iluwatar#107 JavaDoc for Singleton
1 parent 743b6e6 commit 4bf2e3f

File tree

5 files changed

+88
-74
lines changed

5 files changed

+88
-74
lines changed
Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,55 @@
1-
package com.iluwatar.singleton;
2-
3-
/**
4-
*
5-
* Singleton pattern ensures that the class (IvoryTower) can have only one
6-
* existing instance per java classloader instance and provides global access to it.
7-
*
8-
* http://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java
9-
*
10-
* The risk of this pattern is that bugs resulting from setting a singleton up in a distributed environment can
11-
* be tricky to debug, since it will work fine if you debug with a single classloader. Additionally, these
12-
* problems can crop up a while after the implementation of a singleton, since they may start out synchronous and
13-
* only become async with time, so you it may not be clear why you are seeing certain changes in behaviour.
14-
*
15-
* http://stackoverflow.com/questions/17721263/singleton-across-jvm-or-application-instance-or-tomcat-instance
16-
*/
17-
public class App {
18-
19-
public static void main(String[] args) {
20-
21-
// eagerly initialized singleton
22-
IvoryTower ivoryTower1 = IvoryTower.getInstance();
23-
IvoryTower ivoryTower2 = IvoryTower.getInstance();
24-
System.out.println("ivoryTower1=" + ivoryTower1);
25-
System.out.println("ivoryTower2=" + ivoryTower2);
26-
27-
// lazily initialized singleton
28-
ThreadSafeLazyLoadedIvoryTower threadSafeIvoryTower1 = ThreadSafeLazyLoadedIvoryTower
29-
.getInstance();
30-
ThreadSafeLazyLoadedIvoryTower threadSafeIvoryTower2 = ThreadSafeLazyLoadedIvoryTower
31-
.getInstance();
32-
System.out.println("threadSafeIvoryTower1=" + threadSafeIvoryTower1);
33-
System.out.println("threadSafeIvoryTower2=" + threadSafeIvoryTower2);
34-
35-
// enum singleton
36-
EnumIvoryTower enumIvoryTower1 = EnumIvoryTower.INSTANCE;
37-
EnumIvoryTower enumIvoryTower2 = EnumIvoryTower.INSTANCE;
38-
System.out.println("enumIvoryTower1=" + enumIvoryTower1);
39-
System.out.println("enumIvoryTower2=" + enumIvoryTower2);
40-
41-
InitializingOnDemandHolderIdiom demandHolderIdiom = InitializingOnDemandHolderIdiom.getInstance();
42-
System.out.println(demandHolderIdiom);
43-
InitializingOnDemandHolderIdiom demandHolderIdiom2 = InitializingOnDemandHolderIdiom.getInstance();
44-
System.out.println(demandHolderIdiom2);
45-
46-
ThreadSafeDoubleCheckLocking dcl1 = ThreadSafeDoubleCheckLocking.getInstance();
47-
System.out.println(dcl1);
48-
ThreadSafeDoubleCheckLocking dcl2 = ThreadSafeDoubleCheckLocking.getInstance();
49-
System.out.println(dcl2);
50-
}
51-
}
1+
package com.iluwatar.singleton;
2+
3+
/**
4+
*
5+
* Singleton pattern ensures that the class ({@link IvoryTower}) can have only one
6+
* existing instance per Java classloader instance and provides global access to it.
7+
* <p>
8+
* http://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java
9+
*<p>
10+
* The risk of this pattern is that bugs resulting from setting a singleton up in a distributed environment can
11+
* be tricky to debug, since it will work fine if you debug with a single classloader. Additionally, these
12+
* problems can crop up a while after the implementation of a singleton, since they may start out synchronous and
13+
* only become async with time, so you it may not be clear why you are seeing certain changes in behaviour.
14+
* <p>
15+
* http://stackoverflow.com/questions/17721263/singleton-across-jvm-or-application-instance-or-tomcat-instance
16+
*/
17+
public class App {
18+
19+
/**
20+
* Program entry point
21+
* @param args command line args
22+
*/
23+
public static void main(String[] args) {
24+
25+
// eagerly initialized singleton
26+
IvoryTower ivoryTower1 = IvoryTower.getInstance();
27+
IvoryTower ivoryTower2 = IvoryTower.getInstance();
28+
System.out.println("ivoryTower1=" + ivoryTower1);
29+
System.out.println("ivoryTower2=" + ivoryTower2);
30+
31+
// lazily initialized singleton
32+
ThreadSafeLazyLoadedIvoryTower threadSafeIvoryTower1 = ThreadSafeLazyLoadedIvoryTower
33+
.getInstance();
34+
ThreadSafeLazyLoadedIvoryTower threadSafeIvoryTower2 = ThreadSafeLazyLoadedIvoryTower
35+
.getInstance();
36+
System.out.println("threadSafeIvoryTower1=" + threadSafeIvoryTower1);
37+
System.out.println("threadSafeIvoryTower2=" + threadSafeIvoryTower2);
38+
39+
// enum singleton
40+
EnumIvoryTower enumIvoryTower1 = EnumIvoryTower.INSTANCE;
41+
EnumIvoryTower enumIvoryTower2 = EnumIvoryTower.INSTANCE;
42+
System.out.println("enumIvoryTower1=" + enumIvoryTower1);
43+
System.out.println("enumIvoryTower2=" + enumIvoryTower2);
44+
45+
InitializingOnDemandHolderIdiom demandHolderIdiom = InitializingOnDemandHolderIdiom.getInstance();
46+
System.out.println(demandHolderIdiom);
47+
InitializingOnDemandHolderIdiom demandHolderIdiom2 = InitializingOnDemandHolderIdiom.getInstance();
48+
System.out.println(demandHolderIdiom2);
49+
50+
ThreadSafeDoubleCheckLocking dcl1 = ThreadSafeDoubleCheckLocking.getInstance();
51+
System.out.println(dcl1);
52+
ThreadSafeDoubleCheckLocking dcl2 = ThreadSafeDoubleCheckLocking.getInstance();
53+
System.out.println(dcl2);
54+
}
55+
}

singleton/src/main/java/com/iluwatar/singleton/InitializingOnDemandHolderIdiom.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
/**
66
* The Initialize-on-demand-holder idiom is a secure way of
7-
* creating lazy initialize singleton Object in Java.
7+
* creating lazy initialized singleton object in Java.
88
* refer to "The CERT Oracle Secure Coding Standard for Java"
99
* By Dhruv Mohindra, Robert C. Seacord p.378
10-
*
10+
* <p>
1111
* 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.
12+
* This class also shows how to preserve singleton in serialized version of singleton.
1313
*
1414
* @author mortezaadi@gmail.com
1515
*

singleton/src/main/java/com/iluwatar/singleton/ThreadSafeDoubleCheckLocking.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
/**
44
* Double check locking
5-
*
5+
* <p>
66
* http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
7-
*
7+
* <p>
88
* Broken under Java 1.4.
9+
*
910
* @author mortezaadi@gmail.com
1011
*
1112
*/
Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
package com.iluwatar.singleton;
2-
3-
import org.junit.Test;
4-
5-
import com.iluwatar.singleton.App;
6-
7-
public class AppTest {
8-
9-
@Test
10-
public void test() {
11-
String[] args = {};
12-
App.main(args);
13-
}
14-
}
1+
package com.iluwatar.singleton;
2+
3+
import org.junit.Test;
4+
5+
import com.iluwatar.singleton.App;
6+
7+
/**
8+
*
9+
* Application test
10+
*
11+
*/
12+
public class AppTest {
13+
14+
@Test
15+
public void test() {
16+
String[] args = {};
17+
App.main(args);
18+
}
19+
}

singleton/src/test/java/com/iluwatar/singleton/LazyLoadedSingletonThreadSafetyTest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,27 @@
55
/**
66
*
77
* This test case demonstrates thread safety issues of lazy loaded Singleton implementation.
8-
*
8+
* <p>
99
* Out of the box you should see the test output something like the following:
10-
*
10+
* <p>
1111
* Thread=Thread-4 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e
1212
* Thread=Thread-2 creating instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e
1313
* Thread=Thread-0 creating instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e
1414
* Thread=Thread-0 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e
1515
* Thread=Thread-3 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e
1616
* Thread=Thread-1 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@60f330b0
1717
* Thread=Thread-2 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e
18-
*
18+
* <p>
1919
* By changing the method signature of LazyLoadedIvoryTower#getInstance from
20+
* <p><blockquote><pre>
2021
* public static LazyLoadedIvoryTower getInstance()
22+
* </pre></blockquote><p>
2123
* into
24+
* <p><blockquote><pre>
2225
* public synchronized static LazyLoadedIvoryTower getInstance()
26+
* </pre></blockquote><p>
2327
* you should see the test output change to something like the following:
24-
*
28+
* <p>
2529
* Thread=Thread-4 creating instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490
2630
* Thread=Thread-4 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490
2731
* Thread=Thread-0 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490

0 commit comments

Comments
 (0)