设计模式——单例模式

2018-12-07  本文已影响0人  linjiajiam

定义概述:

单例模式是设计模式中一种,它的要求是单例模式实现的类,必须保证全局中只能实例出一个对象,即在全局中只能存在一个该类的对象。实现方式有两种:饿汉式懒汉式

使用场景

  1. 网站浏览次数的计数器,一般使用单例模式设计。因为如果你存在多个计数器,每一个用户的访问都刷新计数器的值,这样在多线程环境下实计数的值是难以同步的。
  2. windows回收站,你可以尝试打开windows回收站,第一次打开弹出新建回收站窗口,但是第二次打开,不会再打开新的窗口了,只会出现已经打开的窗口。

特征:

单例类都有如下特征

  1. 私有的构造方法
  2. 指向自身的静态实例声明
  3. 公有的获取自身实例的静态方法

实现:

1. 一般情况下类的写法。
public class Person {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
public class MainClass {

    public static void main(String[] args) {

        //调用默认构造函数,创建了两个对象
        Person per1 = new Person();
        Person per2 = new Person();
        per1.setName("test1");
        per2.setName("test2");
        System.out.println(per1.hashCode());
        System.out.println(per2.hashCode());
    }
}   
返回如图,可见生成了两个不同的对象
2. 饿汉式
public class PersonEager {

    //创建一个全局静态常量对象。在类加载时就完成了初始化,所以类加载较慢,但获取对象的速度快
    //多线程环境下可以保证实例唯一
    private static final PersonEager personEager = new PersonEager();

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    //私有构造函数,使得外部不能随意访问构造函数创建对象
    private PersonEager(){

    }

    //提供一个公有方法,返回全局静态常量对象。实现单例(即一个对象,全局只有一个实例)
    public static PersonEager getPersonEager(){
        return personEager;
    }
}
public class MainClass {

    public static void main(String[] args) {

        //使用饿汉式,实现全局唯一实例
        PersonEager per3 = PersonEager.getPersonEager();
        PersonEager per4 = PersonEager.getPersonEager();
        per3.setName("test3");
        per4.setName("test4");
        System.out.println(per3.hashCode());
        System.out.println(per4.hashCode());
    }
}   
饿汉式输出结果

下面用20个线程去测试饿汉式是否有线程安全问题,代码如下:

public class MainClassThread {

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 20; i++) {
            new Thread(new Runnable(){
                public void run(){
                    System.out.println(Thread.currentThread().getName() + "-" +PersonEager.getPersonEager().hashCode());
                }
            }).start();
        }
    }
}

测试结果如下,可以看到创建的都是一个对象,并没有出现线程安全问题。


饿汉式多线程测试结果
3. 懒汉式
public class PersonLazy {

    private static int count;

    //创建一个全局静态对象。静态对象会在类加载的时候创建,但此处是创建的null 对象,目的是实现延迟加载
    //多线程环境中无法保证实例唯一
    private  static PersonLazy personLazy = null;

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    //私有构造函数,使得外部不能随意访问构造函数创建对象
    private PersonLazy(){
        System.out.println("PersonLazy 私有的构造方法被实例化 " + (++count) + " 次。");
    }

    //提供一个公有方法,通过判断对象是否被实例化,返回全局静态对象,实现单例(即一个对象,全局只有一个实例)。
    //在调用此方法时,才创建实例。
    //此处无法保证多线程环境下,实例唯一
    public static PersonLazy getPersonLazy(){
        if(personLazy == null){
            personLazy = new PersonLazy();
        }
        return personLazy;
    }
}
public class MainClass {

    public static void main(String[] args) {

        //懒汉式,单线程测试
        PersonLazy per5 = PersonLazy.getPersonLazy();
        PersonLazy per6 = PersonLazy.getPersonLazy();
        per5.setName("test5");
        per6.setName("test6");
        System.out.println(per5.hashCode());
        System.out.println(per6.hashCode());
    }
}   
懒汉式单线程测试结果
public class MainClassThread {

    public static void main(String[] args) throws InterruptedException {
        Runnable task = ()->{
            String threadName = Thread.currentThread().getName();
            System.out.println("线程 " + threadName + "\t => " + PersonLazy.getPersonLazy().hashCode());
        };
        //  模拟多线程环境下使用 PersonLazy 类获得对象
        for(int i=0;i<20000;i++){
            new Thread(task,"" + i).start();
        }
    }
}

下面是多线程环境下懒汉式测试结果,可以看到构造函数被调用过两次,也导致了创建了两个对象。


懒汉式多线程测试结果
3.1. 懒汉式--方法同步锁
    //加上同步,保证线程安全。实现实例唯一。方法同步锁,效率不高
    public static synchronized PersonLazy getPersonLazySync(){
        if(personLazy == null){
            personLazy = new PersonLazy();
        }
        return personLazy;
    }
public class MainClassThread {

    public static void main(String[] args) throws InterruptedException {
        //懒汉式多线程测试--线程安全--效率不高
        Runnable task = ()->{
            String threadName = Thread.currentThread().getName();
            System.out.println("线程 " + threadName + "\t => " + PersonLazy.getPersonLazySync().hashCode());
        };
        // 模拟多线程环境下使用 PersonLazy 类获得对象
        for(int i=0;i<20000;i++){
            new Thread(task,"" + i).start();
        }
    }
}

3.2. 懒汉式--双重检查
public class PersonLazy {

    private static int count;

    //创建一个全局静态对象。静态对象会在类加载的时候创建,但此处是创建的null 对象,目的是实现延迟加载
    //使用volatile关键字防止重排序,因为 new Instance()是一个非原子操作,可能创建一个不完整的实例
    private  static volatile PersonLazy personLazy = null;

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    //私有构造函数,使得外部不能随意访问构造函数创建对象
    private PersonLazy(){
        System.out.println("PersonLazy 私有的构造方法被实例化 " + (++count) + " 次。");
    }

    //局部线程安全,双重检查。效率高,但是方法不简洁
    public static PersonLazy getPersonLazySyncDoubleCheck(){
        //第一重检查
        if(personLazy == null){
            synchronized (PersonLazy.class){
                //第二重检查
                if(personLazy == null){
                    personLazy = new PersonLazy();
                }
            }
        }
        return personLazy;
    }
}
public class MainClassThread {

    public static void main(String[] args) throws InterruptedException {
        //懒汉式多线程测试--线程安全--效率不高
        Runnable task = ()->{
            String threadName = Thread.currentThread().getName();
            System.out.println("线程 " + threadName + "\t => " + PersonLazy.getPersonLazySyncDoubleCheck().hashCode());
        };
        // 模拟多线程环境下使用 PersonLazy 类获得对象
        for(int i=0;i<20000;i++){
            new Thread(task,"" + i).start();
        }
    }
}
3.3. 懒汉式--内部类

虚拟机会保证一个类的<clinit>()方法在多线程环境中被正确地加锁、同步,如果多个线程同时去初始化一个类,那么只会有一个线程去执行这个类的<clinit>()方法,其他线程都需要阻塞等待,直到活动线程执行<clinit>()方法完毕。如果在一个类的<clinit>()方法中有耗时很长的操作,就可能造成多个进程阻塞(需要注意的是,其他线程虽然会被阻塞,但如果执行<clinit>()方法后,其他线程唤醒之后不会再次进入<clinit>()方法。同一个加载器下,一个类型只会初始化一次。

public class PersonLazyInnerClass {

    private static int count;

    // 私有内部类,按需加载,用时加载,也就是延迟加载
    private static class Holder{
        private static PersonLazyInnerClass personLazyInnerClass = new PersonLazyInnerClass();
    }

    private PersonLazyInnerClass(){
        System.out.println("PersonLazy 私有的构造方法被实例化 " + (++count) + " 次。");
    }

    public static PersonLazyInnerClass getPersonLazyInnerClass(){
        return Holder.personLazyInnerClass;
    }
}
public class MainClassThread {

    public static void main(String[] args) throws InterruptedException {
        //懒汉式内部类--线程安全--效率高
        Runnable task = ()->{
            String threadName = Thread.currentThread().getName();
            System.out.println("线程 " + threadName + "\t => " + PersonLazyInnerClass.getPersonLazyInnerClass().hashCode());
        };
        // 模拟多线程环境下使用 PersonLazy 类获得对象
        for(int i=0;i<20000;i++){
            new Thread(task,"" + i).start();
        }
    }
}

效率测试:

方法同步锁 -------- 2339ms
双重检查 -------- 8ms
内部类 ------- 12ms

public class MainClassEffc {

    public static void main(String[] args) throws InterruptedException {
        long startTime=System.currentTimeMillis();

        //效率测试-方法同步锁
        /*Thread t1 = new Thread(new Runnable(){
            public void run(){
                for (int i = 0; i < 100000000; i++) {
                    PersonLazy.getPersonLazySync();
                    //System.out.println(Thread.currentThread().getName() + "-" +PersonLazy.getPersonLazySync().hashCode());
                }
            }
        });*/

        Thread t1 = new Thread(new Runnable(){
            public void run(){
                for (int i = 0; i < 100000000; i++) {
                    PersonLazy.getPersonLazySyncDoubleCheck();
                    //System.out.println(Thread.currentThread().getName() + "-" +PersonLazy.getPersonLazySync().hashCode());
                }
            }
        });

        /*Thread t1 = new Thread(new Runnable(){
            public void run(){
                for (int i = 0; i < 100000000; i++) {
                    PersonLazyInnerClass.getPersonLazyInnerClass();
                    //System.out.println(Thread.currentThread().getName() + "-" +PersonLazy.getPersonLazySync().hashCode());
                }
            }
        });*/


        t1.start();
        t1.join();
        long endTime=System.currentTimeMillis(); //获取结束时间
        System.out.println("结束执行任务!!!!" + (endTime-startTime) + "ms");
    }
}
上一篇 下一篇

猜你喜欢

热点阅读