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

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

饿汉式

  • 类加载到内存后,就实例化一个单例对象,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;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

第二范式

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值