|
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 | +} |
0 commit comments