单例 (JAVA)
java中单例模式是一种常见的设计模式,以下是它的特点:
- 单例类只能有一个实例。
- 单例类必须自己创建自己的唯一实例。
- 单例类必须给所有其他对象提供这一实例
第一种(懒汉,线程不安全):
1 public class Singleton {
2 private static Singleton instance;
3
2 private static Singleton instance;
3
private Singleton (){}
4 public static Singleton getInstance() {
5 if (instance == null) {
6 instance = new Singleton();
7 }
8 return instance;
9 }
10 }
11
4 public static Singleton getInstance() {
5 if (instance == null) {
6 instance = new Singleton();
7 }
8 return instance;
9 }
10 }
11
懒汉式是典型的时间换空间,就是每次获取实例都会进行判断,看是否需要创建实例,浪费判断的时间。当然,如果一直没有人使用的话,那就不会创建实例,则节约内存空间
由于懒汉式的实现是线程安全的,这样会降低整个访问的速度,而且每次都要判断.
第二种(懒汉,线程安全):
1 public class Singleton {
2 private static Singleton instance;
3
2 private static Singleton instance;
3
private Singleton (){}
4 public static synchronized Singleton getInstance() {
5 if (instance == null) {
6 instance = new Singleton();
7 }
8 return instance;
9 }
10 }
11
4 public static synchronized Singleton getInstance() {
5 if (instance == null) {
6 instance = new Singleton();
7 }
8 return instance;
9 }
10 }
11
第三种(饿汉):
1 public class Singleton {
2 private static Singleton instance = new Singleton();
3
2 private static Singleton instance = new Singleton();
3
private Singleton (){}
4 public static Singleton getInstance() {
5 return instance;
6 }
7 }
8
4 public static Singleton getInstance() {
5 return instance;
6 }
7 }
8
第四种(饿汉,变种):
1 public class Singleton {
2 private Singleton instance = null;
3 static {
4 instance = new Singleton();
5 }
6
2 private Singleton instance = null;
3 static {
4 instance = new Singleton();
5 }
6
private Singleton (){}
7 public static Singleton getInstance() {
8 return this.instance;
9 }
10 }
11
7 public static Singleton getInstance() {
8 return this.instance;
9 }
10 }
11
表面上看起来差别挺大,其实更第三种方式差不多,都是在类初始化即实例化instance。
第五种(静态内部类):
1 public class Singleton {
2 private static class SingletonHolder {
3 private static final Singleton INSTANCE = new Singleton();
4 }
5
2 private static class SingletonHolder {
3 private static final Singleton INSTANCE = new Singleton();
4 }
5
private Singleton (){}
6 public static final Singleton getInstance() {
7 return SingletonHolder.INSTANCE;
8 }
9 }
10
6 public static final Singleton getInstance() {
7 return SingletonHolder.INSTANCE;
8 }
9 }
10
优点:加载时不会初始化静态变量INSTANCE,因为没有主动使用,达到Lazy loading
第六种(枚举):
1 public enum Singleton {
2 INSTANCE;
3 public void whateverMethod() {
4 }
5 }
6
2 INSTANCE;
3 public void whateverMethod() {
4 }
5 }
6
优点:不仅能避免多线程同步问题,而且还能防止反序列化
第七种(双重校验锁):
1 public class Singleton {
2 private volatile static Singleton singleton;
3
2 private volatile static Singleton singleton;
3
private Singleton (){}
4 public static Singleton getSingleton() {
5 if (singleton == null) {
6 synchronized (Singleton.class) {
7 if (singleton == null) {
8 singleton = new Singleton();
9 }
10 }
11 }
12 return singleton;
13 }
14 }
15
4 public static Singleton getSingleton() {
5 if (singleton == null) {
6 synchronized (Singleton.class) {
7 if (singleton == null) {
8 singleton = new Singleton();
9 }
10 }
11 }
12 return singleton;
13 }
14 }
15
总结
有两个问题需要注意:
1、如果单例由不同的类装载器装入,那便有可能存在多个单例类的实例。假定不是远端存取,例如一些
servlet容器对每个
servlet使用完全不同的类 装载器,这样的话如果有两个
servlet访问一个单例类,它们就都会有各自的实例。
2、如果
Singleton实现了
java.io.Serializable接口,那么这个类的实例就可能被序列化和复原。不管怎样,如果你序列化一个单例类的对象,接下来复原多个那个对象,那你就会有多个单例类的实例。
对第一个问题修复的办法是:
1 private static Class getClass(String classname)
2 throws ClassNotFoundException {
3 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
4
5 if(classLoader == null)
6 classLoader = Singleton.class.getClassLoader();
7
8 return (classLoader.loadClass(classname));
9 }
10 }
11
2 throws ClassNotFoundException {
3 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
4
5 if(classLoader == null)
6 classLoader = Singleton.class.getClassLoader();
7
8 return (classLoader.loadClass(classname));
9 }
10 }
11
对第二个问题修复的办法是:
1 public class Singleton implements java.io.Serializable {
2 public static Singleton INSTANCE = new Singleton();
3
4 protected Singleton() {
5
6 }
7 private Object readResolve() {
8 return INSTANCE;
9 }
10 }
11
2 public static Singleton INSTANCE = new Singleton();
3
4 protected Singleton() {
5
6 }
7 private Object readResolve() {
8 return INSTANCE;
9 }
10 }
11