Skip to content

Commit 35679af

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 1006d3e + 6d8e7d8 commit 35679af

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/create/create.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
| 包名 | 描述 |
44
| ------- | ----- |
55
| builder | 创建者模式 |
6-
|prototype|原型模式|
6+
|prototype|原型模式|
7+
|singleton|单例模式|
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package create.singleton;
2+
3+
/**
4+
* @author PhxNirvana 2017/3/14 0014.
5+
* 双重锁的单例
6+
*/
7+
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+
if (mInstance == null) {
22+
mInstance = new Singleton();
23+
}
24+
}
25+
}
26+
return mInstance;
27+
}
28+
}

0 commit comments

Comments
 (0)