【设计模式】单例模式
发布日期:2021-05-09 04:18:39 浏览次数:16 分类:博客文章

本文共 5274 字,大约阅读时间需要 17 分钟。

一、前言

  端午小长假冷不丁的就完了,那么接着品设计模式,今天要介绍的设计模式是单例模式,单例模式在编程中的使用很广泛,如线程池、缓存等都可采用单例模式来保证整个系统只有一个实例。然而,如何保证只有唯一一个实例是值得探讨的问题,下面开始介绍单例模式。

二、单例模式定义

  定义:确保一个类只有一个实例,并提供一个全局访问点。

  从定义可知,单例模式可以保证无论是在单线程环境还是多线程环境下,一个类只有唯一一个对象

三、示例说明

  考虑一个系统中存在一个缓存,并且要求该缓存在整个系统中值存在一个实例。

  3.1 v1.0

  在没有考虑多线程的情况下,经过分析后很可能写出如下代码。

  Cache

package com.hust.grid.leesf.singleton;import java.util.concurrent.TimeUnit;public class Cache {    private static Cache cache;            private Cache() {            }        public static Cache getInstance() {        if (cache == null) {            try {                TimeUnit.MILLISECONDS.sleep(500);            } catch (InterruptedException e) {                e.printStackTrace();            }            cache = new Cache();        }        return cache;    } }
View Code

  Main(用于测试)

package com.hust.grid.leesf.singleton;public class Main {    public static void main(String[] args) {            Cache cache = Cache.getInstance();        System.out.println(cache);        for (int i = 0; i < 5; i++) {            cache = Cache.getInstance();            System.out.println(cache);        }    }}
View Code

  运行结果:

com.hust.grid.leesf.singleton.Cache@659e0bfdcom.hust.grid.leesf.singleton.Cache@659e0bfdcom.hust.grid.leesf.singleton.Cache@659e0bfdcom.hust.grid.leesf.singleton.Cache@659e0bfdcom.hust.grid.leesf.singleton.Cache@659e0bfdcom.hust.grid.leesf.singleton.Cache@659e0bfd

  说明:在单线程环境下调用了6次getInstance,getInstance在检查是否为null后,会休眠500ms,这样,更有利于观察,通过结果可知,生成的都是同一个对象,即只生成了一个对象,满足要求,但是在多线程环境下如何呢,下面进行测试。

  3.2 v2.0

  上述的Cache代码不变,改变测试代码,在多线程环境下测试。

  Main(用于测试)

package com.hust.grid.leesf.singleton;public class Main {    public static void main(String[] args) {            for (int i = 0; i < 5; i++) {            new MyThread().start();        }    }        static class MyThread extends Thread {        public void run() {            Cache cache = Cache.getInstance();            System.out.println(cache);        }    }}
View Code

  运行结果

com.hust.grid.leesf.singleton.Cache@20b708e4com.hust.grid.leesf.singleton.Cache@43e8772dcom.hust.grid.leesf.singleton.Cache@793d2cfacom.hust.grid.leesf.singleton.Cache@43e8772dcom.hust.grid.leesf.singleton.Cache@407e3dad

  说明:在多线程环境下,并且由上述结果可知,系统并未只生成一个Cache对象,而是生成了多个,不满足要求。下面对Cache进行修改,使其在多线程环境下也只生成一个Cache对象。

  3.3 v3.0(急切创建实例)

  急切的创建实例,不采用延迟实例化的做法,这种做法依赖JVM在加载这个类时马上创建此唯一的单例,JVM会保证在任何线程访问cache静态变量时,一定先创建此实例。

  Cache

package com.hust.grid.leesf.singleton;public class Cache {    private static Cache cache = new Cache();            private Cache() {            }        public static Cache getInstance() {        return cache;    } }
View Code

  Main与v2.0的Main相同。

  运行结果  

com.hust.grid.leesf.singleton.Cache@43e8772dcom.hust.grid.leesf.singleton.Cache@43e8772dcom.hust.grid.leesf.singleton.Cache@43e8772dcom.hust.grid.leesf.singleton.Cache@43e8772dcom.hust.grid.leesf.singleton.Cache@43e8772d

  说明:在多线程环境下,只生成了唯一一个实例,满足条件。但是,这种方法有一定的缺陷。如在系统运行一段时间后,才需要使用到Cache实例,而非一加载就需要使用。

  3.4 v4.0(同步)

  对比v3.0的急切创建实例而言,v4.0使用延迟加载,即在需要的时候再加载,并且采用同步保证多线程环境下只创建唯一一个对象。

  3.4.1 v4.0.1(同步方法)

  使用同步方法实现单例模式

  Cache

package com.hust.grid.leesf.singleton;public class Cache {    private static Cache cache;            private Cache() {            }        public synchronized static Cache getInstance() {        if (cache == null) {            cache = new Cache();        }        return cache;    } }
View Code

  Main类的代码不变

  运行结果

com.hust.grid.leesf.singleton.Cache@43e8772dcom.hust.grid.leesf.singleton.Cache@43e8772dcom.hust.grid.leesf.singleton.Cache@43e8772dcom.hust.grid.leesf.singleton.Cache@43e8772dcom.hust.grid.leesf.singleton.Cache@43e8772d

  说明:在多线程环境下,只生成了唯一一个实例,满足条件。

  3.4.2 v4.0.2(同步块)

  使用同步块实现单例模式

package com.hust.grid.leesf.singleton;public class Cache {    private static Cache cache;    private Cache() {    }    public static Cache getInstance() {        synchronized (Cache.class) {            if (cache == null) {                cache = new Cache();            }        }        return cache;    }}
View Code

  Main类的代码不变

com.hust.grid.leesf.singleton.Cache@43e8772dcom.hust.grid.leesf.singleton.Cache@43e8772dcom.hust.grid.leesf.singleton.Cache@43e8772dcom.hust.grid.leesf.singleton.Cache@43e8772dcom.hust.grid.leesf.singleton.Cache@43e8772d

  说明:在多线程环境下,只生成了唯一一个实例,满足条件。

  v4.0.1和v4.0.2都存在一个问题,即每次调用getInstance方法时,都需要进行同步处理,实际上,只需要在第一次调用getInstance时进行同步即可,后面再调用时,无需进行同步。

  3.5 v5.0

  使用延迟加载,采用双重检查加锁机制来实现单例模式。

package com.hust.grid.leesf.singleton;public class Cache {    private volatile static Cache cache;    private Cache() {    }    public static Cache getInstance() {        if (cache == null) {            synchronized (Cache.class) {                if (cache == null) {                    cache = new Cache();                                }            }        }        return cache;    }}
View Code

  Main类的代码不变

  运行结果

com.hust.grid.leesf.singleton.Cache@6c6aa204com.hust.grid.leesf.singleton.Cache@6c6aa204com.hust.grid.leesf.singleton.Cache@6c6aa204com.hust.grid.leesf.singleton.Cache@6c6aa204com.hust.grid.leesf.singleton.Cache@6c6aa204

  说明:由结果可知,在多线程环境下,只生成了唯一一个实例,满足条件。cache使用volatile修饰,可以某个线程修改了cache域后,能够对其他线程立即可见。

  当单例模式遇上了不同的类加载器时,还是否会保证单例呢?由于不同的类加载器有自己的类空间,所以会产生多个实例,这时,需要自行制定类加载器,并指定同一个类加载器。

四、总结

  单例模式比较简单,毕竟在生产环境中也有些应用,在面试的时候也会有所涉及,所以掌握单例模式总是好的,所有源代码已经上传至,欢迎fork,谢谢各位园友的观看~ 

 

上一篇:【设计模式】命令模式
下一篇:【JUC】JDK1.8源码分析之SynchronousQueue(九)

发表评论

最新留言

很好
[***.229.124.182]2025年03月30日 11时38分27秒