/** * 单例模式(线程不安全) */ public class SingletonTest { private static SingletonTest instance; private SingletonTest() { } public static SingletonTest getInstance() { if (instance == null) { instance = new SingletonTest(); } return instance; } }
/** * 单例模式(线程安全、每次调用都同步getInstance方法,影响效率) */ public class SingletonTest { private static SingletonTest instance; private SingletonTest() { } public static synchronized SingletonTest getInstance() { if (instance == null) { instance = new SingletonTest(); } return instance; } }
/** * 单例模式(线程安全、JVM加载类时马上创建实例) */ public class SingletonTest { private static SingletonTest instance = new SingletonTest(); private SingletonTest() { } public static SingletonTest getInstance() { return instance; } }
/** * 单例模式(线程安全) */ public class SingletonTest { private volatile static SingletonTest instance; private SingletonTest() { } public static SingletonTest getInstance() { if (instance == null) { synchronized (SingletonTest.class) { if(instance == null) { instance = new SingletonTest(); } } } return instance; } }
95崔国权(mobei) 2025-04-02
95崔国权(mobei) 2025-04-02
95崔国权(mobei) 2025-04-02
藏家519 2025-04-03
马踏飞燕 2025-04-03
1 2025-04-03
彭于晏东北分晏 2025-04-02
藏家574 2025-04-02
一耶暴富 2025-04-02
藏家197 2025-04-02