-
Notifications
You must be signed in to change notification settings - Fork 1
/
T2Singleton.java
119 lines (103 loc) · 3.16 KB
/
T2Singleton.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
package chapter2;
/**
* 单例模式:
*
* Created by 18710 on 2017/9/5.
*/
public class T2Singleton {
/**
* 单例模式,懒汉式,线程安全
* 成员变量是私有静态常量并初始化赋值,构造函数私有,通过 public 方法获取SingleTon类的实例
* 提前就赋值
*/
public static class Singleton {
private final static Singleton INSTANCE = new Singleton();
private Singleton() {}
public static Singleton getInstance() {
return INSTANCE;
}
}
/**
* 单例模式,饿汉式,线程不安全
* 成员变量是私有静态变量并初始化不赋值,构造方法私有,通过 public 方法判断 成员变量是否有值才进行赋值
* 需要时判断赋值
*/
public static class SingleTon2 {
private static SingleTon2 instance = null;
private SingleTon2() {
}
public static SingleTon2 getInstance() {
if (instance == null) {
instance = new SingleTon2();
}
return instance;
}
}
/**
* 单例模式,饿汉式,线程安全,多线程环境下效率高
*/
public static class Singleton3 {
private static Singleton3 instance = null;
private Singleton3() {
}
public synchronized static Singleton3 getInstance() { // 加上同步方法
if (instance == null) {
instance = new Singleton3();
}
return instance;
}
}
/**
* 单例模式,饿汉式,变种,线程安全
*/
public static class Singleton4 {
private static Singleton4 instance = null;
static { // 静态类中声明
instance = new Singleton4();
}
private Singleton4() {
}
public static Singleton4 getInstance() {
return instance;
}
}
/**
* 单例模式,懒汉式,使用静态内部类,线程安全(推荐)
*/
public static class Singleton5 {
private final static class SingletonHolder { // 使用静态内部类
private static final Singleton5 INSTANCE = new Singleton5();
}
private Singleton5() {
}
private static Singleton5 getInstance() {
return SingletonHolder.INSTANCE;
}
}
/**
* 静态内部类,使用枚举方式,线程安全【推荐】
*/
public enum SingleTon6 {
INSTANCE;
public void whateverMethod(){
}
}
/**
* 静态内部类,使用双重校验锁,线程安全【推荐】
*/
public static class Singleton7 {
private volatile static Singleton7 instance = null; // volatile关键字保证原子性
private Singleton7() {
}
public static Singleton7 getInstance() {
if (instance == null) {
synchronized (Singleton7.class) { // synchronized关键字保证线程安全
if (instance == null) {
instance = new Singleton7();
}
}
}
return instance;
}
}
}