单例模式

1、饿汉式

//饿汉式单例模式
public class HungryMan {
    private HungryMan(){  //私有化构造器

    }
    private final static HungryMan instance = new HungryMan();
    public static HungryMan getInstance() {
        return instance;
    }
}

优点:线程安全,使用没用延迟

缺点:启动时即创建实例,启动慢,容易产生垃圾

2、懒汉式

//懒汉式单例模式
public class LazyMan {
    private LazyMan(){  //私有构造方法

    }

    private static LazyMan instance = null;

    public LazyMan getInstance(){
        if (instance == null) {     //需要且不存在的时候再创建实例
            instance = new LazyMan();
        }
        return instance;
    }

}

优点:懒加载启动快,资源占用小

缺点:线程不安全

3、DCL懒汉式(双重检测锁模式)

//DCL懒汉式
public class LazyMan {
    private LazyMan(){  //私有构造方法

    }

    private volatile static LazyMan instance = null;  //原子性

    public LazyMan getInstance(){
        if (instance == null) {
            synchronized (LazyMan.class){       //加锁
                if (instance == null) {
                    instance = new LazyMan();
                }
            }
        }
        return instance;
    }

}

优点:线程安全,懒加载启动快,资源占用小

缺点:synchronized为独占排他锁,并发性能差。即使在创建成功以后,获取实例仍是串行化操作。

4、Holder模式(静态内部类)

//静态内部类懒汉式
public class Holder {
    private Holder(){   //私有化构造器

    }
   private static class InnerClass{ //静态内部类
        private static final Holder instance = new Holder();
   }
   public static Holder getInstance(){
        return InnerClass.instance;
   }
}

优点:将懒加载和线程安全完美结合的一种方式(无锁)。 (较推荐)

5、枚举单例模式

public enum Singleton {
    Instance;

    //可以省略此方法,通过Singleton.Instance进行操作。
    public  Singleton getInstance() {
        return Instance;
    }
}

优点:线程安全

缺点:可读性较差

总结:

枚举单例模式在《Effective Java》中推荐的单例模式之一。但枚举实例在日常开发是很少使用的,就是很简单才导致可读性较差。
在以上所有的单例模式中,推荐静态内部类单例模式。主要是非常直观,即保证线程安全又保证唯一性。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值