设计模式(一)之单例模式

设计模式(一)之单例模式

饿汉式

  • 类加载到内存后,就实例化一个单例对象,JVM保证线程安全
  • 简单实用,推荐使用
  • 唯一缺点:不管用到与否,类加载时完成实例化操作

第一种写法:

声明私有静态常量创建对象

private static final HungryPattern01 INSTANCE = new HungryPattern01();
private HungryPattern01() {
    
    
}
public static HungryPattern01 getInstance() {
    
    
    return INSTANCE;
}

第二种写法:

在静态代码块中创建对象,并对私有静态常量进行赋值

private static final HungryPattern02 INSTANCE;
static {
    
    
    INSTANCE = new HungryPattern02();
}
private HungryPattern02() {
    
    
}
public static HungryPattern02 getInstance() {
    
    
    return INSTANCE;
}

懒汉式(Lazy Coding)

饿汉式不管实例对象有没有被用到,都会使用类加载完成实例化操作,懒汉式虽然达到了按需初始化的目的,但是却带来了线程不安全的问题。

第一种写法:

private static LazyPattern01 INSTANCE;
private LazyPattern01() {
    
    
}
public static LazyPattern01 getInstance() {
    
    
    if (INSTANCE == null) {
    
    
        return new LazyPattern01();
    }
    return INSTANCE;
}

为了体现出这种写法带来的线程不安全问题,为此我进行了简单的多线程测试。

private static LazyPattern01 INSTANCE;
private LazyPattern01() {
    
    
}
public static LazyPattern01 getInstance() {
    
    
    if (INSTANCE == null) {
    
    
        try {
    
    
            Thread.sleep(1);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
        INSTANCE = new LazyPattern01();
    }
    return INSTANCE;
}

//测试方法
@Test
public void test03() {
    
    
    for (int i = 0; i < 100; i++) {
    
    
        new Thread(() -> System.out.println(LazyPattern01.getInstance().hashCode()))
                .start();
    }
}

在这里插入图片描述

第二种写法:

使用synchronized关键字进行加锁,以同步方法的方式来实现线程安全,但这种方式往往效率比较低。

private static LazyPattern02 INSTANCE;
private LazyPattern02() {
    
    
}
public static synchronized LazyPattern02 getInstance() {
    
    
    if (INSTANCE == null) {
    
    
        INSTANCE = new LazyPattern02();
    }
    return INSTANCE;
}

第三种写法:

妄图通过同步代码块的方式提高效率,然而并不可行。

private static LazyPattern03 INSTANCE;
private LazyPattern03() {
    
    
}
public static LazyPattern03 getInstance() {
    
    
    if (INSTANCE == null) {
    
    
        synchronized (LazyPattern03.class) {
    
    
            INSTANCE = new LazyPattern03();
        }
    }
    return INSTANCE;
}

第四种写法:

在第三种写法的基础上,增加了双重判断。

private static volatile LazyPattern04 INSTANCE;
private LazyPattern04() {
    
    
}
public static LazyPattern04 getInstance() {
    
    
    if (INSTANCE == null) {
    
    
        synchronized (LazyPattern04.class) {
    
    
            if (INSTANCE == null) {
    
    
                INSTANCE = new LazyPattern04();
            }
        }
    }
    return INSTANCE;
}

第五种写法:

采用静态内部类方式,JVM保证单例,加载外部类时不会加载内部类,这样就可以实现懒加载。

private LazyPattern05() {
    
    
}
private static class LazyPattern05Holder {
    
    
    private static final LazyPattern05 INSTANCE = new LazyPattern05();
}
public static LazyPattern05 getInstance() {
    
    
    return LazyPattern05Holder.INSTANCE;
}

第六种写法:

《Effective Java》中推荐使用枚举实现单例模式,堪称最完美的单例写法。

不仅可以解决线程同步的问题,还可以防止反序列化。

public enum LazyPattern06 {
    
    
    INSTANCE;
}

在这里插入图片描述

Guess you like

Origin blog.csdn.net/weixin_44191814/article/details/121908226