单例设计模式

单例设计模式

1、 什么是单例?
单例:在JVM执行的过程中,只能有一个实例。
2、 单例模式分为饿汉式和懒汉式
一、饿汉式:天生线程安全,因为饿汉式是在class被加载的时候创建的对象,所以不存在线程安全问题。由于懒汉式加了同步代码块,所以饿汉式的效率比懒汉式要高
二、懒汉式:我们需要创建对象的时候才创建对象,比较节省内存,由于加了同步代码块保证线程安全,所以效率很变低。

饿汉式单例的实现

class HungrySingleton{
	//在class被JVM加载时创建单例对象,static是保证只有一个对象
	private static final HungrySingleton singleton = new HungrySingleton();
	/**
	 * 私有化构造器的目的是为了不让别人new对象
	 * 反射也不可以创建对象了
	 */
	private HungrySingleton() {
		
	}
	/**
	 * 创建一个获取单例对象的方法
	 */
	public static HungrySingleton getHungrySingleton(){
		return singleton;
	}
}

public class HungrySingletonTest {
	public static void main(String[] args) {
		HungrySingleton s1 = HungrySingleton.getHungrySingleton();
		HungrySingleton s2 = HungrySingleton.getHungrySingleton();
		System.out.println(s1);
		System.out.println(s2);
		System.out.println(s1==s2);
	}
}

实现结果
在这里插入图片描述

如果不使用私有构造器

class HungrySingleton{
	//在class被JVM加载时创建单例对象,static是保证只有一个对象
	private static final HungrySingleton singleton = new HungrySingleton();
	/**
	 * 创建一个获取单例对象的方法
	 */
	public static HungrySingleton getHungrySingleton(){
		return singleton;
	}
}

public class HungrySingletonTest {
	public static void main(String[] args) {
		HungrySingleton s1 = HungrySingleton.getHungrySingleton();
		HungrySingleton s2 = HungrySingleton.getHungrySingleton();
		HungrySingleton s3 = new HungrySingleton();
		System.out.println(s1);
		System.out.println(s2);
		System.out.println(s3);
	}
}

输出结果
在这里插入图片描述

懒汉式单例的实现

我没有加synchronized,向大家展示一下线程安全问题

/**
 * 懒汉式单例
 * @author 紫炎易霄
 */
class LazySingleton{
	//加static保证只有一个实例
	private static LazySingleton singleton;
	/**
	 * 私有化构造器保证不能被人new
	 */
	private LazySingleton(){
		
	}
	/**
	 * 提供一个获得单例的方法
	 */
	public static  LazySingleton getLazySingleton(){
		if(singleton == null){
			singleton = new LazySingleton();
		}
		return singleton;
	}
}
class TreadLazySingletion implements Runnable{
	@Override
	public void run() {
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		for (int i = 0; i < 5; i++) {
			LazySingleton lazySingleton = LazySingleton.getLazySingleton();
			System.out.println(lazySingleton);
		}
	}
	
}

public class LazySingletonTest {
	public static void main(String[] args) {
		TreadLazySingletion treadLazySingletion = new TreadLazySingletion();
		Thread t1 = new Thread(treadLazySingletion);
		Thread t2 = new Thread(treadLazySingletion);
		t1.start();
		t2.start();
	}
}

运行结果
在这里插入图片描述
加锁之后(大家可以多试试运行结果,我运行了十几次没有发生线程安全问题)

/**
 * 懒汉式单例
 * @author 紫炎易霄
 */
class LazySingleton{
	//加static保证只有一个实例
	private static LazySingleton singleton;
	/**
	 * 私有化构造器保证不能被人new
	 */
	private LazySingleton(){
		
	}
	/**
	 * 提供一个获得单例的方法
	 */
	public static  LazySingleton getLazySingleton(){
		synchronized (LazySingleton.class) {
			if(singleton == null){
					singleton = new LazySingleton();
			}
		}
		return singleton;
	}
}
class TreadLazySingletion implements Runnable{
	@Override
	public void run() {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		for (int i = 0; i < 50; i++) {
			LazySingleton lazySingleton = LazySingleton.getLazySingleton();
			System.out.println(lazySingleton);
		}
	}
	
}

public class LazySingletonTest {
	public static void main(String[] args) {
		TreadLazySingletion treadLazySingletion = new TreadLazySingletion();
		Thread t1 = new Thread(treadLazySingletion);
		Thread t2 = new Thread(treadLazySingletion);
		t1.start();
		t2.start();
	}
}

运行结果
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值