标签:类装载 enum 基于 实例化 private 饿汉 推荐 多线程 利用
类的单例设计模式,就是采取一定的方法来保证在整个软件系统中,某个类只存在一个对象实例。且该类只提供一个取得其对象实例的方法(静态方法)。
单例模式有八种方式:
步骤大致如下:
代码:
package singleton.type1;
public class Singleton {
    private Singleton() {
        
    }
    
    private static final Singleton instance = new Singleton();
    
    public static Singleton getInstance() {
        return instance;
    }
    
}测试代码:
package singleton.type1;
public class SingletonTest {
    public static void main(String[] args) {
        Singleton instance = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance == instance2);
        System.out.println(instance.hashCode());
        System.out.println(instance2.hashCode());
    }
}测试结果:
true
2018699554
2018699554说明
结论:此模式可用,可能造成内存浪费
代码:
package singleton.type2;
public class Singleton {
    private Singleton() {
        
    }
    
    private static final Singleton instance;
    
    static {
        instance = new Singleton();
    }
    
    public static Singleton getInstance() {
        return instance;
    }
    
}测试代码和测试结果同上
说明
结论:此模式可用,可能造成内存浪费
代码:
package singleton.type3;
public class Singleton {
    private Singleton() {
        
    }
    
    private static Singleton instance;
    
    public static Singleton getInstance() {
        if(null == instance) {
            instance = new Singleton();
        }
        return instance;
    }
    
}说明
结论:实际开发中,不要用这种方式
代码:
package singleton.type4;
public class Singleton {
    private Singleton() {
        
    }
    
    private static Singleton instance;
    
    public static synchronized Singleton getInstance() {
        if(null == instance) {
            instance = new Singleton();
        }
        return instance;
    }
    
}说明
结论:实际开发中,不推荐使用这种方式
代码:
package singleton.type5;
public class Singleton {
    private Singleton() {
        
    }
    
    private static Singleton instance;
    
    public static Singleton getInstance() {
        if(null == instance) {
            synchronized(Singleton.class) {             
                instance = new Singleton();
            }
        }
        return instance;
    }
    
}说明
结论:实际开发中, 不能使用这种方式
代码:
package singleton.type6;
public class Singleton {
    private Singleton() {
        
    }
    
    private static volatile Singleton instance;
    
    public static Singleton getInstance() {
        if(null == instance) {
            synchronized(Singleton.class) {
                if(null == instance) {                  
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
    
}这里需要额外说明一下,volatile关键字使得某个线程对某个对象(变量)的修改,其他线程能立刻感知到。
说明
结论:实际开发中,推荐使用这种单例设计模式
代码:
package singleton.type7;
public class Singleton {
    private Singleton() {
        
    }
    
    private static class SingletonInstance {
        private static final Singleton INSTANCE = new Singleton();
    }
    
    public static Singleton getInstance() {
        return SingletonInstance.INSTANCE;
    }
    
}说明
结论:推荐使用
代码:
package singleton.type8;
public enum Singleton {
    INSTANCE;
    
}说明
结论:推荐使用
在JDK中,java.lang.Runtime就是经典的单例模式(饿汉式)

标签:类装载 enum 基于 实例化 private 饿汉 推荐 多线程 利用
原文地址:https://www.cnblogs.com/tenny-peng/p/11579144.html