
本文共 2752 字,大约阅读时间需要 9 分钟。
������������������������������������������������������
���������������Singleton Pattern���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
���������������������������
���������������������������������������������������������greedy singleton������������������lazy singleton������
���������
������������Greedy Singleton������������������������������������������������������������������������������������������������������������������������������������������������������������������������
private static Singleton uniqueInstance = new Singleton();
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
���������
������������Lazy Singleton���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������������������������������������������������������������������Double Checked Locking���������������
public static synchronized Singleton getInstance() { if (instance == null) { synchronized (Singleton.class) { if (instance == null) { instance = new Singleton(); } } } return instance;}
���������������������������������������������������������������volatile
���
private volatile static Singleton instance = null;
������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
package example6;public enum Singleton { UNIQUE_INSTANCE;}
���������������������������������������������������������������������������������������������������������������������������������������
������������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
