单例模式的八种写法

写法一:饿汉式,静态常量

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

    public Singleton1() {
    }

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

优点:在类装载的时候就实例化,避免了线程同步问题

缺点:没有达到Lazy Loading的效果

结论:可用,不推荐

写法二:饿汉式,静态代码块

public class Singleton2 {
    private static Singleton2 instance;

    private Singleton2() {
    }

    static {
        instance = new Singleton2();
    }

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

此种写法和写法一没有本质区别

结论:可用,不推荐

写法三:懒汉式,线程不安全

public class Singleton3 {
    private static Singleton3 instance;

    private Singleton3() {
    }

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

优点:达到了Lazy Loading的效果

缺点:多线程环境下,一个线程进入了if (instance == null) 判断语句块,还未来得及往下执行,另一个线程也通过了这个判断语句,这是会产生多个实例。线程不安全。

结论:不可用

写法四:懒汉式,线程安全,同步方法

public class Singleton4 {
    private static Singleton4 instance;

    private Singleton4() {
    }

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

优点:线程安全

缺点:效率太低,每个线程想要获得类的实例的时候,执行getInstance()方法,都要进行同步。而实际上这个方法只执行一次实例化代码就够了,后面想要获得该类的实例,直接return就行了。

结论:可用,不推荐

写法五:懒汉式,线程安全,同步代码块

public class Singleton5 {
    private static Singleton5 instance;

    private Singleton5() {
    }

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

优点:这种方式本意是对第四种方法的改进

缺点:但是这种写法并不能起到线程同步的作用。假设一个线程进入了if (instance == null)判断,还未来得及往下执行,另一个线程也通过了这个判断,这是就会产生多个实例

结论:不可用

写法六:双重检查

public class Singleton6 {
    private static volatile Singleton6 instance;

    private Singleton6() {
    }

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

优点:Double-Check改变是多线程开发中经常用到的,乳代码所示,我们进行了两次if (instance == null)检查,这样就保证了线程安全。实例化代码只执行一次,后面再次访问时,判断if (instance == null),直接return实例化对象,也避免了反复进行方法同步。

结论:推荐使用

写法七:静态内部类

public class Singleton7 {
    private Singleton7() {
    }

    private static class SingletonInstance {
        private static final Singleton7 INSTANCE = new Singleton7();
    }

    public static Singleton7 getInstance() {
        return SingletonInstance.INSTANCE;
    }
}

优点:采用了类装载的机制保证初始化实例时只有一个线程。静态内部类方式在Singleton7类被装载时并不会立即实例化,而是在需要实例化时,调用getInstance方法,才会装载SingletonInstance类,从而完成Singleton的实例化。

结论:推荐使用

写法八:枚举

public enum Singleton8 {
    INSTANCE;
}

优点:借助了JDK1.5中添加的枚举来实例化单例模式。不仅避免了线程同步问题,而且还能防止反序列化重新创建新的对象。

结论:推荐使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值