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