Please wait...

小波Note

四川 · 成都市10 ℃
English

Java Singleton

成都 (cheng du)8/26/2024, 5:33:50 PM2.91kEstimated reading time 9 minFavoriteCtrl + D / ⌘ + D
cover
IT FB(up 主)
Back-end development engineer

饿汉式单例 (Eager Initialization)

java
        public class EagerSingleton {
    private static final EagerSingleton instance = new EagerSingleton();

    private EagerSingleton() {}

    public static EagerSingleton getInstance() {
        return instance;
    }
}

    

懒汉式单例 (Lazy Initialization)

java
        public class LazySingleton {
    private static LazySingleton instance;

    private LazySingleton() {}

    public static synchronized LazySingleton getInstance() {
        if (instance == null) {
            instance = new LazySingleton();
        }
        return instance;
    }
}

    

双重检查锁定 (Double-Check Locking)

java
        public class DoubleCheckLockingSingleton {
    private volatile static DoubleCheckLockingSingleton instance;

    private DoubleCheckLockingSingleton() {}

    public static DoubleCheckLockingSingleton getInstance() {
        if (instance == null) {
            synchronized (DoubleCheckLockingSingleton.class) {
                if (instance == null) {
                    instance = new DoubleCheckLockingSingleton();
                }
            }
        }
        return instance;
    }
}

    

静态内部类 (Static Inner Class)

java
        public class StaticInnerClassSingleton {
    private StaticInnerClassSingleton() {}

    private static class SingletonHolder {
        private static final StaticInnerClassSingleton instance = new StaticInnerClassSingleton();
    }

    public static StaticInnerClassSingleton getInstance() {
        return SingletonHolder.instance;
    }
}

    

枚举 (Enum)

java
        public enum EnumSingleton {
    INSTANCE;

    public void whateverMethod() {
    }
}

    

注册式单例 (Registry)

java
        public class RegistrySingleton {
    private static Map<String, RegistrySingleton> map = new ConcurrentHashMap<>();

    static {
        RegistrySingleton instance = new RegistrySingleton();
        map.put(instance.getClass().getName(), instance);
    }

    protected RegistrySingleton() {}

    public static RegistrySingleton getInstance(String name) {
        if (name == null) {
            name = RegistrySingleton.class.getName();
        }
        if (map.get(name) == null) {
            try {
                map.put(name, (RegistrySingleton) Class.forName(name).newInstance());
            } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return map.get(name);
    }
}

    

ThreadLocal

java
        public class ThreadLocalSingleton {
    private static final ThreadLocal<ThreadLocalSingleton> threadLocalInstance = ThreadLocal.withInitial(ThreadLocalSingleton::new);

    private ThreadLocalSingleton() {}

    public static ThreadLocalSingleton getInstance() {
        return threadLocalInstance.get();
    }
}