深入探讨5种单例模式

深入探讨5种单例模式

一、对比总览

以下是不同单例模式实现方式的特性对比表格。表格从线程安全性、延迟加载、实现复杂度、反序列化安全性、防反射攻击性等多个方面进行考量。

特性 饿汉式 饱汉式 饱汉式-双检锁 静态内部类 枚举单例(推荐)
线程安全性 ×
延迟加载 × ×
实现复杂度 ×
反序列化安全性 × × × ×
防反射攻击性 × × × ×

详细解释

  1. 线程安全性

    • 饿汉式:类加载时即创建实例(因为instance是static),线程安全。
    • 饱汉式:未使用同步机制,线程不安全。
    • 饱汉式-双检锁:使用同步块和双重检查锁,线程安全。
    • 静态内部类:通过类加载机制,线程安全。
    • 枚举单例:JVM确保线程安全。
  2. 延迟加载

    • 饿汉式:类加载时即创建实例,不具备延迟加载。
    • 饱汉式:实例在首次使用时创建,具备延迟加载。
    • 饱汉式-双检锁:实例在首次使用时创建,具备延迟加载。
    • 静态内部类:实例在首次使用时创建(因为静态内部类只有在使用时才会加载),具备延迟加载。
    • 枚举单例:类加载时即创建实例,不具备延迟加载。
  3. 实现复杂度

    • 饿汉式:实现简单。
    • 饱汉式:实现简单。
    • 饱汉式-双检锁:实现相对复杂。
    • 静态内部类:实现简单。
    • 枚举单例:实现简单。
  4. 反序列化安全性

    • 饿汉式饱汉式饱汉式-双检锁静态内部类 :需要实现 readResolve 方法以防止反序列化创建新实例。
    • 枚举单例:天然防止反序列化创建新实例,JVM保证。
  5. 防反射攻击性

    • 饿汉式饱汉式饱汉式-双检锁静态内部类:可能通过反射创建新实例。
    • 枚举单例 :防止反射攻击,创建新实例时抛出 IllegalArgumentException

二、代码

1. 饿汉式

language-java
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
public class OrderManager1 {

@Getter
@Setter
private Map<String, TradeOrder> orders = new HashMap<>();

@Getter
private static final OrderManager1 instance = new OrderManager1();

/**
* 添加订单
*
* @param order 顺序
*/
public void addOrder(TradeOrder order) {

orders.put(order.getId(), order);
}

/**
* 获取订单
*
* @param orderId 订单id
* @return {@link TradeOrder}
*/
public TradeOrder getOrder(String orderId) {

return orders.get(orderId);
}
}

2. 饱汉式

language-java
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
public class OrderManager2 {

@Getter
@Setter
private Map<String, TradeOrder> orders = new HashMap<>();

private static OrderManager2 instance;

public static OrderManager2 getInstance(){

if (instance == null){

instance = new OrderManager2();
}
return instance;
}

/**
* 添加订单
*
* @param order 顺序
*/
public void addOrder(TradeOrder order) {

orders.put(order.getId(), order);
}

/**
* 获取订单
*
* @param orderId 订单id
* @return {@link TradeOrder}
*/
public TradeOrder getOrder(String orderId) {

return orders.get(orderId);
}
}

3. 饱汉式-双检锁

language-java
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
public class OrderManager3 {

@Getter
@Setter
private Map<String, TradeOrder> orders = new HashMap<>();

private static OrderManager3 instance;

public static OrderManager3 getInstance(){

if (instance == null){

synchronized (OrderManager3.class){

if (instance == null){

instance = new OrderManager3();
}
}
instance = new OrderManager3();
}
return instance;
}

/**
* 添加订单
*
* @param order 顺序
*/
public void addOrder(TradeOrder order) {

orders.put(order.getId(), order);
}

/**
* 获取订单
*
* @param orderId 订单id
* @return {@link TradeOrder}
*/
public TradeOrder getOrder(String orderId) {

return orders.get(orderId);
}
}

4. 静态内部类

language-java
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
public class OrderManager4 {

@Getter
@Setter
private Map<String, TradeOrder> orders = new HashMap<>();

private static class SingletonHelper{

private static final OrderManager4 INSTANCE = new OrderManager4();
}

public static OrderManager4 getInstance(){

return SingletonHelper.INSTANCE;
}


/**
* 添加订单
*
* @param order 顺序
*/
public void addOrder(TradeOrder order) {

orders.put(order.getId(), order);
}

/**
* 获取订单
*
* @param orderId 订单id
* @return {@link TradeOrder}
*/
public TradeOrder getOrder(String orderId) {

return orders.get(orderId);
}
}

5. 枚举单例

language-java
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
public enum OrderManager5 {

INSTANCE;

@Getter
@Setter
private Map<String, TradeOrder> orders = new HashMap<>();

/**
* 添加订单
*
* @param order 顺序
*/
public void addOrder(TradeOrder order) {

orders.put(order.getId(), order);
}

/**
* 获取订单
*
* @param orderId 订单id
* @return {@link TradeOrder}
*/
public TradeOrder getOrder(String orderId) {

return orders.get(orderId);
}
}

三、性能对比

language-java
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package org.dragon.singleton;

import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

@Slf4j
public class SingletonPerformanceTest {

static long timeout = 20; // 超时时间,单位为秒
static int testIterations = 10_000_000; // 测试次数
static int threadCount = 1000; // 并发线程数
static Map<String, HashMap<String, Long>> result = new HashMap<>();

public static void main(String[] args) {

/*
* 多次调用,结果是最后一次调用存入。为什么多次调用,因为单次test不准确,总是靠前的OrderManager跑的快,可能是因为Java某些机制导致的
* 所以多次调用,逐渐平稳。
* */
firstCreationTest();
mulAccessTest();
mulAccessTest();
mulAccessTest();
ConcurrentAccessTest();
ConcurrentAccessTest();
printRes();
ConcurrentAccessTest();
printRes();
ConcurrentAccessTest();
printRes();
}

/**
* 打印结果
*/
private static void printRes(){

ArrayList<String> names = new ArrayList<>();
names.add(OrderManager1.class.getSimpleName());
names.add(OrderManager2.class.getSimpleName());
names.add(OrderManager3.class.getSimpleName());
names.add(OrderManager4.class.getSimpleName());
names.add(OrderManager5.class.getSimpleName());
// 表头
System.out.printf("%-20s%-20s%-25s%-25s%-20s%n", "Singleton Type", "First Creation (ms)", "Multiple Access (ms)", "Concurrent Access (ms)", "Memory Used (MB)");
System.out.println("---------------------------------------------------------------------------------------------------------------");

for (String name : names) {

// 打印结果,转换时间为毫秒
System.out.printf("%-20s%-20.3f%-25.3f%-25.3f%-20.3f%n", name, result.get(name).get("firstCreation") / 1_000_000.0, result.get(name).get("mulAccess") / 1_000_000.0, result.get(name).get("ConcurrentAccess") / 1_000_000.0, 0 / (1024.0 * 1024.0));
}
}

/**
* 首次创建测试
*/
private static void firstCreationTest(){

List<Runnable> tests = new ArrayList<>();
tests.add(()->firstCreation(OrderManager1::getInstance));
tests.add(()->firstCreation(OrderManager2::getInstance));
tests.add(()->firstCreation(OrderManager3::getInstance));
tests.add(()->firstCreation(OrderManager4::getInstance));
tests.add(()->firstCreation(() -> OrderManager5.INSTANCE));
// 随机化测试顺序
Collections.shuffle(tests);
//run
for (Runnable test : tests) {

test.run();
log.info("Complete one test");
try {

Thread.sleep(200);
} catch (InterruptedException e) {

throw new RuntimeException(e);
}
}
}

/**
* 多次访问测试
*/
private static void mulAccessTest(){

List<Runnable> tests = new ArrayList<>();
tests.add(()->mulAccess(OrderManager1::getInstance, testIterations));
tests.add(()->mulAccess(OrderManager2::getInstance, testIterations));
tests.add(()->mulAccess(OrderManager3::getInstance, testIterations));
tests.add(()->mulAccess(OrderManager4::getInstance, testIterations));
tests.add(()->mulAccess(() -> OrderManager5.INSTANCE, testIterations));
// 随机化测试顺序
Collections.shuffle(tests);
//run
for (Runnable test : tests) {

test.run();
log.info("Complete one test");
try {

Thread.sleep(200);
} catch (InterruptedException e) {

throw new RuntimeException(e);
}
}
}

/**
* 多线程访问测试
*/
private static void ConcurrentAccessTest(){

List<Runnable> tests = new ArrayList<>();
tests.add(()->ConcurrentAccess(OrderManager1::getInstance, testIterations, threadCount));
tests.add(()->ConcurrentAccess(OrderManager2::getInstance, testIterations, threadCount));
tests.add(()->ConcurrentAccess(OrderManager3::getInstance, testIterations, threadCount));
tests.add(()->ConcurrentAccess(OrderManager4::getInstance, testIterations, threadCount));
tests.add(()->ConcurrentAccess(() -> OrderManager5.INSTANCE, testIterations, threadCount));
// 随机化测试顺序
Collections.shuffle(tests);
//run
for (Runnable test : tests) {

test.run();
log.info("Complete one test");
try {

Thread.sleep(200);
} catch (InterruptedException e) {

throw new RuntimeException(e);
}
}
}

/**
* 首次创建
*
* @param singletonSupplier 单一供应商
* @return long ns
*/
private static <T> long firstCreation(Supplier<T> singletonSupplier){

// 测试首次创建时间
long startTime = System.nanoTime();
T instance = singletonSupplier.get();
long endTime = System.nanoTime();
long resTime = endTime - startTime;
//save res
String simpleName = instance.getClass().getSimpleName();
HashMap<String, Long> resMap = result.computeIfAbsent(simpleName, k->new HashMap<>());
resMap.put("firstCreation", resTime);
return resTime;
}

/**
* 多次访问
*
* @param singletonSupplier 单一供应商
* @param iterations 迭代
* @return long ns
*/
private static <T> long mulAccess(Supplier<T> singletonSupplier, int iterations){

//预热
for (int i = 0; i < 100_000; i++) {

T instance = singletonSupplier.get();
}
//计算
long startTime = System.nanoTime();
for (int i = 0; i < iterations; i++) {

T instance = singletonSupplier.get();
}
long endTime = System.nanoTime();
long resTime = endTime - startTime;
//save res
String simpleName = singletonSupplier.get().getClass().getSimpleName();
HashMap<String, Long> resMap = result.computeIfAbsent(simpleName, k->new HashMap<>());
resMap.put("mulAccess", resTime);
return resTime;
}

/**
* 并发访问
*
* @param singletonSupplier 单一供应商
* @param iterations 迭代
* @param threadCount 线程数
* @return long ns
*/
private static <T> long ConcurrentAccess(Supplier<T> singletonSupplier, int iterations, int threadCount){

ExecutorService executorService = Executors.newFixedThreadPool(100);
//预热
CountDownLatch latch1 = new CountDownLatch(100);
for (int i = 0; i < threadCount; i++) {

executorService.submit(() -> {

for (int j = 0; j < 100_000; j++) {

T instance = singletonSupplier.get();
}
latch1.countDown();
});
}
try {

boolean completed = latch1.await(timeout, TimeUnit.SECONDS);
if (!completed) {

System.out.println("Concurrent access test for 预热" + singletonSupplier.get().getClass().getSimpleName() + " timed out!");
}
} catch (InterruptedException e) {

e.printStackTrace();
}
//计算
CountDownLatch latch2 = new CountDownLatch(threadCount);
long startTime = System.nanoTime();
for (int i = 0; i < threadCount; i++) {

executorService.submit(() -> {

for (int j = 0; j < iterations; j++) {

T instance = singletonSupplier.get();
}
latch2.countDown();
});
}
try {

boolean completed = latch2.await(timeout, TimeUnit.SECONDS);
if (!completed) {

System.out.println("Concurrent access test for " + singletonSupplier.getClass().getSimpleName() + " timed out!");
}
} catch (InterruptedException e) {

e.printStackTrace();
}
long endTime = System.nanoTime();
long concurrentAccessTime = endTime - startTime;

executorService.shutdown();
//save res
String simpleName = singletonSupplier.get().getClass().getSimpleName();
HashMap<String, Long> resMap = result.computeIfAbsent(simpleName, k->new HashMap<>());
resMap.put("ConcurrentAccess", concurrentAccessTime);
return concurrentAccessTime;
}
}

结果输出如下

language-txt
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
62
63
[17:15:54.519] [INFO ] org.dragon.singleton.SingletonPerformanceTest 73 firstCreationTest - Complete one test
[17:15:54.730] [INFO ] org.dragon.singleton.SingletonPerformanceTest 73 firstCreationTest - Complete one test
[17:15:54.936] [INFO ] org.dragon.singleton.SingletonPerformanceTest 73 firstCreationTest - Complete one test
[17:15:55.141] [INFO ] org.dragon.singleton.SingletonPerformanceTest 73 firstCreationTest - Complete one test
[17:15:55.347] [INFO ] org.dragon.singleton.SingletonPerformanceTest 73 firstCreationTest - Complete one test
[17:15:55.554] [INFO ] org.dragon.singleton.SingletonPerformanceTest 97 mulAccessTest - Complete one test
[17:15:55.782] [INFO ] org.dragon.singleton.SingletonPerformanceTest 97 mulAccessTest - Complete one test
[17:15:56.007] [INFO ] org.dragon.singleton.SingletonPerformanceTest 97 mulAccessTest - Complete one test
[17:15:56.227] [INFO ] org.dragon.singleton.SingletonPerformanceTest 97 mulAccessTest - Complete one test
[17:15:56.445] [INFO ] org.dragon.singleton.SingletonPerformanceTest 97 mulAccessTest - Complete one test
[17:15:56.669] [INFO ] org.dragon.singleton.SingletonPerformanceTest 97 mulAccessTest - Complete one test
[17:15:56.906] [INFO ] org.dragon.singleton.SingletonPerformanceTest 97 mulAccessTest - Complete one test
[17:15:57.146] [INFO ] org.dragon.singleton.SingletonPerformanceTest 97 mulAccessTest - Complete one test
[17:15:57.376] [INFO ] org.dragon.singleton.SingletonPerformanceTest 97 mulAccessTest - Complete one test
[17:15:57.598] [INFO ] org.dragon.singleton.SingletonPerformanceTest 97 mulAccessTest - Complete one test
[17:15:57.818] [INFO ] org.dragon.singleton.SingletonPerformanceTest 97 mulAccessTest - Complete one test
[17:15:58.054] [INFO ] org.dragon.singleton.SingletonPerformanceTest 97 mulAccessTest - Complete one test
[17:15:58.276] [INFO ] org.dragon.singleton.SingletonPerformanceTest 97 mulAccessTest - Complete one test
[17:15:58.495] [INFO ] org.dragon.singleton.SingletonPerformanceTest 97 mulAccessTest - Complete one test
[17:15:58.716] [INFO ] org.dragon.singleton.SingletonPerformanceTest 97 mulAccessTest - Complete one test
[17:15:59.430] [INFO ] org.dragon.singleton.SingletonPerformanceTest 121 ConcurrentAccessTest - Complete one test
[17:15:59.658] [INFO ] org.dragon.singleton.SingletonPerformanceTest 121 ConcurrentAccessTest - Complete one test
[17:16:02.737] [INFO ] org.dragon.singleton.SingletonPerformanceTest 121 ConcurrentAccessTest - Complete one test
[17:16:08.533] [INFO ] org.dragon.singleton.SingletonPerformanceTest 121 ConcurrentAccessTest - Complete one test
[17:16:13.700] [INFO ] org.dragon.singleton.SingletonPerformanceTest 121 ConcurrentAccessTest - Complete one test
[17:16:19.432] [INFO ] org.dragon.singleton.SingletonPerformanceTest 121 ConcurrentAccessTest - Complete one test
[17:16:24.632] [INFO ] org.dragon.singleton.SingletonPerformanceTest 121 ConcurrentAccessTest - Complete one test
[17:16:30.366] [INFO ] org.dragon.singleton.SingletonPerformanceTest 121 ConcurrentAccessTest - Complete one test
[17:16:35.516] [INFO ] org.dragon.singleton.SingletonPerformanceTest 121 ConcurrentAccessTest - Complete one test
[17:16:40.431] [INFO ] org.dragon.singleton.SingletonPerformanceTest 121 ConcurrentAccessTest - Complete one test
Singleton Type First Creation (ms) Multiple Access (ms) Concurrent Access (ms) Memory Used (MB)
---------------------------------------------------------------------------------------------------------------
OrderManager1 0.010 16.848 4928.236 0.000
OrderManager2 0.010 18.592 5504.781 0.000
OrderManager3 0.009 19.127 5513.309 0.000
OrderManager4 0.241 18.772 4920.940 0.000
OrderManager5 0.117 16.637 4704.835 0.000
[17:16:45.002] [INFO ] org.dragon.singleton.SingletonPerformanceTest 121 ConcurrentAccessTest - Complete one test
[17:16:48.982] [INFO ] org.dragon.singleton.SingletonPerformanceTest 121 ConcurrentAccessTest - Complete one test
[17:16:52.778] [INFO ] org.dragon.singleton.SingletonPerformanceTest 121 ConcurrentAccessTest - Complete one test
[17:16:57.834] [INFO ] org.dragon.singleton.SingletonPerformanceTest 121 ConcurrentAccessTest - Complete one test
[17:17:02.298] [INFO ] org.dragon.singleton.SingletonPerformanceTest 121 ConcurrentAccessTest - Complete one test
Singleton Type First Creation (ms) Multiple Access (ms) Concurrent Access (ms) Memory Used (MB)
---------------------------------------------------------------------------------------------------------------
OrderManager1 0.010 16.848 4217.333 0.000
OrderManager2 0.010 18.592 4340.869 0.000
OrderManager3 0.009 19.127 4824.581 0.000
OrderManager4 0.241 18.772 3743.032 0.000
OrderManager5 0.117 16.637 3558.995 0.000
[17:17:06.778] [INFO ] org.dragon.singleton.SingletonPerformanceTest 121 ConcurrentAccessTest - Complete one test
[17:17:11.231] [INFO ] org.dragon.singleton.SingletonPerformanceTest 121 ConcurrentAccessTest - Complete one test
[17:17:15.733] [INFO ] org.dragon.singleton.SingletonPerformanceTest 121 ConcurrentAccessTest - Complete one test
[17:17:20.812] [INFO ] org.dragon.singleton.SingletonPerformanceTest 121 ConcurrentAccessTest - Complete one test
[17:17:25.837] [INFO ] org.dragon.singleton.SingletonPerformanceTest 121 ConcurrentAccessTest - Complete one test
Singleton Type First Creation (ms) Multiple Access (ms) Concurrent Access (ms) Memory Used (MB)
---------------------------------------------------------------------------------------------------------------
OrderManager1 0.010 16.848 4281.649 0.000
OrderManager2 0.010 18.592 4849.279 0.000
OrderManager3 0.009 19.127 4782.224 0.000
OrderManager4 0.241 18.772 4267.228 0.000
OrderManager5 0.117 16.637 4233.907 0.000

进程已结束,退出代码为 0

可以去多跑几次,基本上最后一种枚举单例,性能属于最好的一批。并且也最安全。