We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 1006d3e + 6d8e7d8 commit 35679afCopy full SHA for 35679af
src/create/create.md
@@ -3,4 +3,5 @@
3
| 包名 | 描述 |
4
| ------- | ----- |
5
| builder | 创建者模式 |
6
-|prototype|原型模式|
+|prototype|原型模式|
7
+|singleton|单例模式|
src/create/singleton/Singleton.java
@@ -0,0 +1,28 @@
1
+package create.singleton;
2
+
+/**
+ * @author PhxNirvana 2017/3/14 0014.
+ * 双重锁的单例
+ */
8
+public class Singleton {
9
+ //防止外部创建实例
10
+ private Singleton() {
11
12
+ }
13
+ //唯一实例
14
+ private static volatile Singleton mInstance;
15
16
+ public static Singleton getInstance() {
17
+ //第一个锁,如果没有实例
18
+ if (mInstance == null) {
19
+ synchronized (Singleton.class) {
20
+ //第二个锁,如果没有任何线程创建Singleton实例
21
22
+ mInstance = new Singleton();
23
24
25
26
+ return mInstance;
27
28
+}
0 commit comments