Java单例模式

单例模式:一个对象只能创建一个实例。

为了确保单例实例的唯一性,所有的单例构造器都要被声明为私有的,再通过声明静态方法实现全局访问获得该单例实例。

懒汉式单例模式

public class Singleton {
	//懒汉式这里没有马上new instance
	private static Singleton instance = null;
	
	private Singleton(){
		System.out.println();
	}
	
	private static Singleton getInstance(){
		if(instance == null){
			instance = new Singleton();
		}
		return instance;
	}
}

懒汉式可以理解为懒,没有马上用到,没有马上实例化,等调用了getInstance时,才去实例化。

饿汉式单例模式

public class Singleton {
	//饿汉式这里马上实例化
	private static Singleton instance =new Singleton();
	
	private Singleton(){
		System.out.println();
	}
	
	private static Singleton getInstance(){
		return instance;
	}
	
}

饿汉式可以理解为饥不择食,一运行马上就实例化了。

懒汉式是线程不安全的,饿汉式是线程安全的。

饿汉式为啥是线程安全的
private static Singleton instance =new Singleton();

饿汉式由static修饰,并且实例化交给了自己的引用。由于单例类被加载时,就已经实例化了。一个类只加载一次,所以这个只会创建唯一的一个实例。也就是严格的线程安全。

懒汉式为啥是线程不安全的
private static Singleton instance = null;

懒汉式在类加载的时候实例的引用是null,开始并没有实例化所以引用为null。
在多线程中用到懒汉式,可能会出现,第一个线程调用了构造函数实例化单例对象,而此时第二个线程来了,也会去检查实例是不是null,但是这个时候第一个线程还没有完成实例化操作,所以第二个线程得到的实例也是null,也会开始实例化单例对象。

同步锁 synchronized

有两种方式
第一种是声明synchronized 关键字

private static synchronized  Singleton getInstance()
public class Singleton {
	//懒汉式这里没有马上new instance
	private static Singleton instance = null;
	
	private Singleton(){
		System.out.println();
	}
	
	private static synchronized Singleton getInstance(){
		if(instance == null){
			instance = new Singleton();
		}
		return instance;
	}
}

第二种是synchronized 方法块

synchronized(Singleton.class){
	if(instance == null){
	instance = new Singleton();
	}
}
public class Singleton {
	//懒汉式这里没有马上new instance
	private static Singleton instance = null;
	
	private Singleton(){
		System.out.println();
	}
	
	private static  Singleton getInstance(){
		synchronized (Singleton.class) {
			if(instance == null){
				instance = new Singleton();
			}
		}
		return instance;
	}
}

上面的方法能够带来线程的安全,但是随之而来的是延迟。synchronized是线程同步的,如果实例已经创建了,同步锁就不起作用了。这里就需要双重校验的单例模式了。

双重检验机制的单例模式

if(instance == null){
		synchronized (Singleton.class) {
			if(instance == null){
				instance = new Singleton();
			}
		}
	}
public class Singleton {
	//懒汉式这里没有马上new instance
	private static Singleton instance = null;
	
	private Singleton(){
		System.out.println();
	}
	
	private static  Singleton getInstance(){
		if(instance == null){
		synchronized (Singleton.class) {
			if(instance == null){
				instance = new Singleton();
			}
		}
		}
		return instance;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值