1. 懒汉式线程不安全
public class LazyUnsafeSingleton { private static LazyUnsafeSingleton singleton; private LazyUnsafeSingleton ( ) { } public static LazyUnsafeSingleton getInstance ( ) { if ( singleton == null ) singleton = new LazyUnsafeSingleton ( ) ; return singleton; }
}
2. 饿汉式(线程安全)
public class HungrySingleton { private static HungrySingleton singleton = new HungrySingleton ( ) ; private HungrySingleton ( ) { } public static HungrySingleton getInstance ( ) { return singleton; }
}
3. 懒汉式线程安全
volatile
synchronized
public class LazySafeSingleton { private static volatile LazySafeSingleton singleton; private LazySafeSingleton ( ) { } public static synchronized LazySafeSingleton getInstance ( ) { if ( singleton == null ) singleton = new LazySafeSingleton ( ) ; return singleton; }
}
4. 双重检查锁
public class DoubleCheckedLockSingleton { private static volatile DoubleCheckedLockSingleton singleton; private DoubleCheckedLockSingleton ( ) { } public static DoubleCheckedLockSingleton getInstance ( ) { if ( singleton == null ) { synchronized ( DoubleCheckedLockSingleton . class ) { if ( singleton == null ) { singleton = new DoubleCheckedLockSingleton ( ) ; } } } return singleton; }
}
5. 静态内部类
public class StaticInnerClassSingleton { private static StaticInnerClassSingleton singleton; private static class SingletonHolder { private static StaticInnerClassSingleton INSTANCE = new StaticInnerClassSingleton ( ) ; } public static StaticInnerClassSingleton getInstance ( ) { return SingletonHolder . INSTANCE ; }
}
6. 枚举类型
public enum EnumSingleton { SINGLETON ; public String doSomeThing ( ) { return "hello world" ; }
}
测试类
public class SingletonTest { public static void main ( String [ ] args) throws NoSuchMethodException , IllegalAccessException , InvocationTargetException , InstantiationException , InterruptedException { LazyUnsafeSingleton lazyUnsafeSingleton = LazyUnsafeSingleton . getInstance ( ) ; HungrySingleton hungrySingleton = HungrySingleton . getInstance ( ) ; LazySafeSingleton lazySafeSingleton = LazySafeSingleton . getInstance ( ) ; DoubleCheckedLockSingleton doubleCheckedLockSingleton = DoubleCheckedLockSingleton . getInstance ( ) ; StaticInnerClassSingleton staticInnerClassSingleton = StaticInnerClassSingleton . getInstance ( ) ; EnumSingleton enumSingleton = EnumSingleton . SINGLETON ; enumSingleton. doSomeThing ( ) ; Class < EnumSingleton > enumSingletonClass = EnumSingleton . class ;
Constructor < ? > declaredConstructor = enumSingletonClass. getDeclaredConstructors ( ) [ 0 ] ; declaredConstructor. setAccessible ( true ) ; Object enumSingleton1 = declaredConstructor. newInstance ( ) ; Class < LazyUnsafeSingleton > lazyUnsafeSingletonClass = LazyUnsafeSingleton . class ; Constructor < LazyUnsafeSingleton > constructor = lazyUnsafeSingletonClass. getDeclaredConstructor ( ) ; constructor. setAccessible ( true ) ; LazyUnsafeSingleton lazyUnsafeSingletonFromReflect = constructor. newInstance ( ) ; System . out. println ( "Original Instance: " + LazyUnsafeSingleton . getInstance ( ) ) ; System . out. println ( "New Instance: " + lazyUnsafeSingletonFromReflect) ; System . out. println ( "Are instances equal? " + ( LazyUnsafeSingleton . getInstance ( ) == lazyUnsafeSingletonFromReflect) ) ; }
}