这里写自定义目录标题
单例设计模式介绍
所谓类的单例设计模式,就是采取一定的方法保证在整个的软件系统中,对某个类只能存在一个对象实例,并且该类只提供一个取得其对象实例的方法(静态方法)。实现单例模式的一个简单方法是,使构造函数私有化,并且创建一个静态方法来完成对象的初始化,这样,对象将在一次调用时创建,此后,这个类将返回同一个对象。
在许多实际应用中,我们只需要创建一个对象,如线程池、缓存、对话框、注册表设置等,如果我们为每个应用程序创建多个实例,则会导致资源的的过度使用,单例模式在这种请路况下是一种经过时间考验的成熟方法,能够在不带来太多缺陷的情况下提供全局访问。
单例模式应用场景
1、日志记录
使用一个日志类的对象,将多项服务的日志信息按照顺序转存到一个特定的日志文件中
2、数据库操作
我们可能希望使用一个数据库对象对数据库进行操作,以维护数据的一致性;
3、打印机后台处理程序
该程序运行过程中只能生成一个实例,以避免对同一资源产生相互冲突的请求。
单例设计模式八种方式
1) 饿汉式(静态常量)
2) 饿汉式(静态代码块)
3) 懒汉式(线程不安全)
4) 懒汉式(线程安全,同步方法)
5) 懒汉式(线程安全,同步代码块)
6) 双重检查
7) 静态内部类
8) 枚举
1.1 饿汉式(静态常量)
public class StaticSingle {
// private构造器外部不能new
private StaticSingle(){
}
//本类内部创建实例对象
public static final StaticSingle instance=new StaticSingle();
//提供公有的静态方法返回实例
public static StaticSingle getInstance(){
return instance;
}
}
class Test{
public static void main(String[] args) {
StaticSingle instance = StaticSingle.getInstance();
StaticSingle instance2 = StaticSingle.getInstance();
System.out.println("instance hashcode="+instance.hashCode());
System.out.println("instance2 hashcode="+instance2.hashCode());
System.out.println(instance2==instance);
}
}
测试结果:
优缺点说明:
- 优点:这种写法比较简单,就是在类装载的时候就完成实例化。避免了线程同步问题。
- 缺点:在类装载的时候就完成实例化,没有达到Lazy Loading的效果。如果从始至终从未使用过这个实例,则会造成内存的浪费
- 这种方式基于classloder机制避免了多线程的同步问题,不过,instance在类装载时就实例化,在单例模式中大多数都是调用getInstance方法, 但是导致类装载的原因有很多种,因此不能确定有其他的方式(或者其他的静态方法)导致类装载,这时候初始化instance就没有达到lazy loading的效果
- 结论:这种单例模式可用,但是可能造成内存浪费
1.2 饿汉式(静态代码块)
public class TestSingle {
public static void main(String[] args) {
SingleType singleType = SingleType.getSingleType();
SingleType singleType2 = SingleType.getSingleType();
System.out.println("instance hashcode="+singleType.hashCode());
System.out.println("instance2 hashcode="+singleType2.hashCode());
System.out.println(singleType.hashCode()==singleType2.hashCode());
}
}
class SingleType{
private SingleType(){
}
public static SingleType instance;
// 通过静态代码块来实例化
static{
instance=new SingleType();
}
//静态方法
public static SingleType getSingleType(){
return instance;
}
}
测试结果:
优缺点说明:
- 这种方式和上面的方式其实类似,只不过将类实例化的过程放在了静态代码块中,也是在类装载的时候,就执行静态代码块中的代码,初始化类的实例。优缺点和上面是一样的。
- 结论:这种单例模式可用,但是可能造成内存浪费
1.3懒汉式(线程不安全)
public class TestSingleTon {
public static void main(String[] args) {
singleTon1 instance = singleTon1.getInstance();
singleTon1 instance2 = singleTon1.getInstance();
System.out.println("instance hashCode="+instance.hashCode());
System.out.println("instance2 hashCode="+instance2.hashCode());
System.out.println(instance==instance2);
}
}
class singleTon1{
private singleTon1(){
}
public static singleTon1 instance;
//懒汉式要用到instance的时候才会创建
//符合lazyloading加载 线程不安全
public static singleTon1 getInstance(){
if (instance == null) {
instance=new singleTon1();
}
return instance;
}
}
测试结果:
优缺点说明:
- 起到了Lazy Loading的效果,但是只能在单线程下使用。
- 如果在多线程下,一个线程进入了if (singleton == null)判断语句块,还未来得及往下执行,另一个线程也通过了这个判断语句,这时便会产生多个实例。所以在多线程环境下不可使用这种方式。
- 结论:在实际开发中,不要使用这种方式
1.4懒汉式(线程安全,同步方法)
public class TestSingleTon2 {
public static void main(String[] args) {
singleTon1 instance = singleTon1.getInstance();
singleTon1 instance2 = singleTon1.getInstance();
System.out.println("instance hashCode="+instance.hashCode());
System.out.println("instance2 hashCode="+instance2.hashCode());
System.out.println(instance==instance2);
}
}
class singleTon{
private singleTon(){
}
public static singleTon instance;
//懒汉式要用到instance的时候才会创建
//符合lazyloading加载 线程安全
public static synchronized singleTon getInstance(){
if (instance == null) {
instance=new singleTon();
}
return instance;
}
}
测试结果:
优缺点说明:
- 解决了线程不安全问题
- 效率太低了,每个线程在想获得类的实例时候,执行getInstance()方法都要进行同步。而其实这个方法只执行一次实例化代码就够了,后面的想获得该类实例,直接return就行了。方法进行同步效率太低
- 结论:在实际开发中,不推荐使用这种方式
1.5懒汉式(线程安全,同步代码块)
class singleTon{
private singleTon(){
}
public static singleTon instance;
//懒汉式要用到instance的时候才会创建
//符合lazyloading加载 线程安全
public static synchronized singleTon getInstance(){
if (instance == null) {
synchronized (singleTon.class) {
instance = new singleTon();
}
}
return instance;
}
}
优缺点说明:
- 这种方式,本意是想对第四种实现方式的改进,因为前面同步方法效率太低,改为同步产生实例化的的代码块
- 但是这种同步并不能起到线程同步的作用。跟第3种实现方式遇到的情形一致,假如一个线程进入了if (singleton == null)判断语句块,还未来得及往下执行,另一个线程也通过了这个判断语句,这时便会产生多个实例
- 结论:在实际开发中,不能使用这种方式
1.6双重检查
public class TestSingleTon3 {
public static void main(String[] args) {
singleTon instance = singleTon.getInstance();
singleTon instance2 = singleTon.getInstance();
System.out.println("双重检查------");
System.out.println(instance2==instance);
System.out.println("instance hashCode="+instance.hashCode());
System.out.println("instance2 hashCode="+instance2.hashCode());
}
}
class singleTon{
private singleTon(){
}
public volatile static singleTon instance;
//符合lazyloading加载 线程安全
public static singleTon getInstance(){
if (instance == null) {
if (instance == null) {
synchronized (singleTon.class){
instance=new singleTon();
}
}
}
return instance;
}
}
结果:
优缺点说明:
- Double-Check概念是多线程开发中常使用到的,如代码中所示,我们进行了两次if (singleton == null)检查,这样就可以保证线程安全了。
- 这样,实例化代码只用执行一次,后面再次访问时,判断if (singleton == null),直接return实例化对象,也避免的反复进行方法同步.
- 线程安全;延迟加载;效率较高
- 结论:在实际开发中,推荐使用这种单例设计模式
1.7静态内部类
public class TestSingleTon4 {
public static void main(String[] args) {
singleTon instance = singleTon.getInstance();
singleTon instance2 = singleTon.getInstance();
System.out.println("静态内部类------");
System.out.println(instance2==instance);
System.out.println("instance hashCode="+instance.hashCode());
System.out.println("instance2 hashCode="+instance2.hashCode());
}
}
class singleTon{
private singleTon(){
}
//静态内部类在外部类加载的时候不会加载只有在使用到的时候才会加载
public static class SingleTonStatic{
public static final singleTon INSTANCE=new singleTon();
}
//符合lazyloading加载 线程安全
public static singleTon getInstance(){
return SingleTonStatic.INSTANCE;
}
}
运行结果:
优缺点说明:
- 这种方式采用了类装载的机制来保证初始化实例时只有一个线程。
- 静态内部类方式在Singleton类被装载时并不会立即实例化,而是在需要实例化时,调用getInstance方法,才会装载SingletonInstance类,从而完成Singleton的实例化。
- 类的静态属性只会在第一次加载类的时候初始化,所以在这里,JVM帮助我们保证了线程的安全性,在类进行初始化时,别的线程是无法进入的。
- 优点:避免了线程不安全,利用静态内部类特点实现延迟加载,效率高
- 结论:推荐使用.
1.8枚举
public class TestEnum {
public static void main(String[] args) {
SingleTon instance = SingleTon.INSTANCE;
SingleTon instance2 = SingleTon.INSTANCE;
System.out.println("instance hashcode="+instance.hashCode());
System.out.println("instance2 hashcode="+instance2.hashCode());
System.out.println(instance2==instance);
}
}
enum SingleTon{
INSTANCE;
}
运行结果:
优缺点说明:
- 这借助JDK1.5中添加的枚举来实现单例模式。不仅能避免多线程同步问题,而
且还能防止反序列化重新创建新的对象。 - 这种方式不能通过反射的方式来调用。
- 结论:推荐使用
源码分析
在JDK中,java.lang.Runtime就是经典的单例模式(饿汉式)源码如图:
单例模式注意事项和细节说明
- 单例模式保证了 系统内存中该类只存在一个对象,节省了系统资源,对于一些需要频繁创建销毁的对象,使用单例模式可以提高系统性能
- 当想实例化一个单例类的时候,必须要记住使用相应的获取对象的方法,而不是使用new
- 单例模式使用的场景:需要频繁的进行创建和销毁的对象、创建对象时耗时过多或耗费资源过多(即:重量级对象),但又经常用到的对象、工具类对象、频繁访问数据库或文件的对象(比如数据源、session工厂等)
本文参考尚硅谷韩顺平老师的Java设计模式