-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleton.java
More file actions
25 lines (22 loc) · 591 Bytes
/
Singleton.java
File metadata and controls
25 lines (22 loc) · 591 Bytes
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
package basic;
/**
* @Author: ChangXuan
* @Decription: 单例模式
* 双重校验锁
* @Date: 15:07 2020/11/15
**/
public class Singleton {
// 使用 volatile 禁止JVM指令重排序
private volatile static Singleton uniqueInstance;
private Singleton(){}
public static Singleton getUniqueInstance() {
if (uniqueInstance == null) {
synchronized (Singleton.class) {
if (uniqueInstance == null) {
uniqueInstance = new Singleton();
}
}
}
return uniqueInstance;
}
}