-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingletonTest.java
More file actions
168 lines (123 loc) · 3.62 KB
/
SingletonTest.java
File metadata and controls
168 lines (123 loc) · 3.62 KB
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
package Gof.Singleton;
public class SingletonTest {
public static void main(String[] args) {
Singleton7 instance1 = Singleton7.getInstance();
Singleton7 instance2 = Singleton7.getInstance();
System.out.println(instance1.hashCode());
System.out.println(instance2.hashCode());
Singleton8 s1 = Singleton8.INSTACNE;
Singleton8 s2 = Singleton8.INSTACNE;
System.out.println(s1 == s2);
}
}
//饿汉式(静态常量)
class SingleTon1 {
private SingleTon1() {
}
private final static SingleTon1 instance = new SingleTon1();
public static SingleTon1 getInstance() {
return instance;
}
}
//饿汉式(静态代码块)
class SingleTon2 {
private SingleTon2() {
}
private static SingleTon2 instance;
static {
instance = new SingleTon2();
}
public static SingleTon2 getInstance() {
return instance;
}
}
//懒汉式(线程不安全)
class Singleton3 {
private static Singleton3 instance;
private Singleton3() {
}
public static Singleton3 getInstance() {
if (instance == null) {
instance = new Singleton3();
}
return instance;
}
}
//懒汉式(线程安全,同步方法)
class Singleton4 {
private static Singleton4 instance;
private Singleton4() {
}
public static synchronized Singleton4 getInstance() {
if (instance == null)
instance = new Singleton4();
return instance;
}
}
//懒汉式(同步代码块,不推荐)
class Singleton5 {
private static Singleton5 instance;
private Singleton5() {
}
public static Singleton5 getInstance() {
if (instance == null)
synchronized (Singleton5.class) {
instance = new Singleton5();
}
return instance;
}
}
//双重检查,同步代码块,线程安全
class Singleton6 {
private static volatile Singleton6 instance;
private Singleton6() {
}
//提供了一个静态的共有方法,加入了双重检查,解决了线程安全问题,同时解决了懒加载问题,保证了效率,推荐
public static Singleton6 getInstance() {
if (instance == null) {
synchronized (Singleton6.class) {
if (instance == null)
instance = new Singleton6();
}
}
return instance;
}
}
//静态内部类
class Singleton7 {
private Singleton7() {
}
private static class SingletonInstance {
private static final Singleton7 INSTANCE = new Singleton7();//final可以不加
}
public static Singleton7 getInstance() {
return SingletonInstance.INSTANCE;
}
}
//枚举
enum Singleton8 {
INSTACNE;//属性
public void sayOK() {
System.out.println("ok");
}
}
// TODO 双重检查锁模式的一种写法
class Singleton9 {
//静态的单例不用volatile修饰
private static Singleton9 instance;
private Singleton9() {
}
//提供了一个静态的共有方法,加入了双重检查,解决了线程安全问题,同时解决了懒加载问题,保证了效率,推荐
public static Singleton9 getInstance() {
if (instance == null) {
synchronized (Singleton9.class) {
if (instance == null) {
//静态单例不用volatile修饰时,可以先创建一个临时实例,然后将该实例赋值给静态单例
Singleton9 tmp = new Singleton9();
instance = tmp;
}
}
}
return instance;
}
}