forked from echoTheLiar/JavaCodeAcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleton.java
More file actions
30 lines (22 loc) · 617 Bytes
/
Singleton.java
File metadata and controls
30 lines (22 loc) · 617 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
26
27
28
29
30
package designpattern.singleton;
/**
* 单例类,使用饿汉式,线程安全(不存在同步问题,但是类被加载即被初始化,特定条件下耗费内存);注释为饱汉式,存在线程不安全的问题
*
* @author liu yuning
*
*/
public class Singleton {
// 饱汉式
// private static Singleton instance;
// 饿汉式
private static final Singleton instance = new Singleton();
private Singleton() {
}
public static Singleton getInstance() {
// 饱汉式
// if (instance == null) {
// instance = new Singleton();
// }
return instance;
}
}