设计模式-单例模式

kevin Lv2

单例模式

单例-保证一个类的具体的实例在整个应用程序中只有唯一的一个

  1. 构造私有化
  2. 提供这个类的唯一(静态)实例
  3. 提供一个公开的静态方法来返回这个实例

1. 饿汉式

单线程/多线程场景下都是安全的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package tech.aistar.design.Singleton.v1;

/**
* @Description 饿汉式单例
* @Author: kevin
* @Date: 2023-07-27
*/
public class Singleton {
//提供唯一的一个实例
private static final Singleton singleton = new Singleton();

//1. 构造私有化
private Singleton() {

}

//提供一个接口
public static Singleton getInstance() {
return singleton;
}
}

class SingletonTest {
public static void main(String[] args) {
Singleton singleton = Singleton.getInstance();
Singleton singleton2 = Singleton.getInstance();
System.out.println(singleton2 == singleton);
}
}

2. 懒汉式

单线程下是ok的,多线程下是不ok的 无法在多线程下保证这个实例是唯一的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package tech.aistar.design.Singleton.v2;

/**
* @Description 懒汉式
* @Author: kevin
* @Date: 2023-07-27
*/
public class Singleton {

private static Singleton singleton;

// 构造私有化
private Singleton() {

}

// 懒汉式,当getSingleton第一次被调用的时候才去创建实例
public static Singleton getSingleton() {
return singleton == null ? singleton = new Singleton() : singleton;
}
}

class SingletonTest {
public static void main(String[] args) {
Singleton singleton = Singleton.getSingleton();
Singleton singleton2 = Singleton.getSingleton();
System.out.println(singleton2 == singleton);
}
}

3. 剖析线程不安全

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package tech.aistar.design.Singleton.v2;

/**
* @Description 懒汉式
* @Author: kevin
* @Date: 2023-07-27
*/
public class Singleton {

private static Singleton instance;

private Singleton() {
System.out.println("构造块");
}

// 懒汉式,当getSingleton第一次被调用的时候才去创建实例
public static Singleton getInstance() {
return instance == null ? instance = new Singleton() : instance;
}
}
class SingletonTest {
public static void main(String[] args) {
//模拟一个多线程的场景
for (int i = 0; i < 100; i++) {
Thread t = new Thread(() -> {
Singleton s1 = Singleton.getInstance();
});
t.start();
}
}
}

1
2
3
4
5
6
7
构造块
构造块
构造块
构造块
构造块
构造块
构造块

4. 懒汉式-线程安全

使用同步锁synchronized

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package tech.aistar.design.Singleton.v2;

/**
* @Description 懒汉式
* @Author: kevin
* @Date: 2023-07-27
*/
public class Singleton {

private static Singleton singleton;

// 构造私有化
private Singleton() {

}

// 懒汉式,当getSingleton第一次被调用的时候才去创建实例
public static synchronized Singleton getSingleton() {
return singleton == null ? singleton = new Singleton() : singleton;
}
}

class SingletonTest {
public static void main(String[] args) {
Singleton singleton = Singleton.getSingleton();
Singleton singleton2 = Singleton.getSingleton();
System.out.println(singleton2 == singleton);
}
}

5. 饿汉式/懒汉式的区别

  1. 线程安全性方面

    饿汉模式 - 单线程/多线程 - 都是线程安全的 - 因为类加载的时候(线程对象还不存在!),就会初始化那个单例的实例.
    
    懒汉模式 - 需要通过加锁的方式,来实现多线程安全.
    
  2. 效率方面 - 懒汉模式通常是需要加锁的,所以涉及到频繁加锁和释放锁的动作,而”锁”也是一个比较昂贵的资源对象[占内存]

    效率不如饿汉模式.

  3. 占内存方面 - 饿汉模式比较占内存 - 类加载的时候,它在内存中就存在了. 懒汉模式是你需要使用它实例的时候,它才会第一次实例化.

6. 双重检测锁-笔试/面试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package tech.aistar.design.Singleton.v5;

/**
* @Description 懒汉式- 正真开发中使用的 volatile关键字 禁止指令重排
* @Author: kevin
* @Date: 2023-07-27
*/
public class Singleton {

private static volatile Singleton instance;

private Singleton() {
System.out.println("构造块");
}


public static Singleton getInstance() {

//创建对象的标准的流程 - "自认为的"
//①申请空间,②初始化,调用构造方法(是一个完整的对象),③将对象在堆空间的内存地址赋值给引用变量

//TODO... 实际的顺序,jvm会进行指令重排,重排后的顺序①-③-②

//N=100个线程
//假设10个线程可以同时判断为null

//等第一个线程执行①-③之后,另外90个线程进来了,判断不为null,直接return instance
//但是这个90个线程拿到的是一个不完整对象的引用,然后可能就去使用这个不完整的对象 - 造成不可预期的结果.


if (instance == null) {

//10个线程竞争锁资源
synchronized (Singleton.class) {//skip

//其中的一个线程进入...
if (instance == null) {

//按照指令重排后的顺序, 第一个线程进入之后按照①-③ , instance已经不为null
//此时instance指向的那个对象仍然是一个不完整的对象,因为还没有来得及执行完构造.
instance = new Singleton();
}
}
//抢到锁的线程会立即释放锁资源
}
return instance;
}
}

class SingletonTest {
public static void main(String[] args) {
//模拟一个多线程的场景
for (int i = 0; i < 100; i++) {
Thread t = new Thread(() -> {
Singleton s1 = Singleton.getInstance();
});
t.start();
}
}
}

  • 标题: 设计模式-单例模式
  • 作者: kevin
  • 创建于: 2023-07-27 14:18:58
  • 更新于: 2023-07-27 16:09:19
  • 链接: https://lsmallice.github.io/posts/69e7b86f.html
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
 评论