public class Singleton {
public static final Singleton INSTANCE;
private String info;
static{
try {
Properties pro = new Properties();
pro.load(Singleton.class.getClassLoader().getResourceAsStream("single.properties"));
INSTANCE = new Singleton(pro.getProperty("info"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private Singleton(){}
public String getInfo() {
return info;
}
}
懒汉式
延迟创建对象。
线程不安全
适用于单线程。
public class Singleton {
private static Singleton instance;
private Singleton(){
}
public static Singleton getInstance(){
if(instance == null){
instance = new Singleton();
}
return instance;
}
}
线程安全 - 双重检测
适用于多线程。
public class Singleton {
private static Singleton instance;
private Singleton(){
}
public static Singleton getInstance(){
if(instance == null){
synchronized (Singleton.class) {
if(instance == null){
instance = new Singleton();
}
}
}
return instance;
}
}
网上有人说 instance 应该加上 volatile 修饰,因为由于指令重排序,导致对象被 new 出来,赋值给 instance 后,但是还没有初始化就被使用了。实际上,这个只有在很低版本的 JVM 才有这个问题,高版本已经解决这个问题了,把 new 操作和初始化做成原子操作。
线程安全 - 静态内部类
适用于多线程。
原理:静态内部类不会自动随着外部类的加载和初始化而初始化。
public class Singleton {
private Singleton(){}
private static class Inner{
private static final Singleton INSTANCE = new Singleton();
}
public static Singleton getInstance(){
return Inner.INSTANCE;
}
}
public class Singleton {
private static final ConcurrentHashMap instances =
new ConcurrentHashMap<>();
private Singleton(){}
public static Singleton getInstance(){
Long currentThreadId = Thread.currentThread().getId();
instances.putIfAbsent(currentThreadId, new IdGenerator());
return instances.get(currentThreadId);
}
}
public class Singleton {
private static SharedObjectStorage storage = new FileSharedObjectStorage();
private static DistributedLock lock = new DistributedLock();
private static Singleton instance;
private Singleton(){}
public synchronized static Singleton getInstance() {
if (instance == null) {
lock.lock();
instance = storage.load(Singleton.class);
}
return instance;
}
public synchroinzed void freeInstance() {
storage.save(this, Singleton.class);
instance = null; //释放对象 lock.unlock();
}
}
多例模式
多例指的是一个类可以创建多个对象,但是个数有限,比如 3 个。
public class Singleton {
private static final int SERVER_COUNT = 3;
private static final Map<Long, Singleton> instances = new HashMap<>();
static {
instances.put(1L, new instances("a"));
instances.put(2L, new instances("b"));
instances.put(3L, new instances("c"));
}
private String name;
private instances(String name) {
this.name = name;
}
public Singleton getInstance(long serverNo) {
return instances.get(serverNo);
}
public Singleton getRandomInstance() {
Random r = new Random();
int no = r.nextInt(SERVER_COUNT)+1;
return instances.get(no);
}
}