单例

/** 线程安全,但效率低  懒汉,线程安全

private static ImAddressPool instance = null;

private ImAddressPool() {}

public synchronized static ImAddressPool getInstance() {

if (instance == null) {

instance = new ImAddressPool();

instance.init();

}

return instance;

}

**/

// 类装载时就实例化 饿汉

/***

private static ImAddressPool instance = new ImAddressPool();

private ImAddressPool() {

init();

}

public static ImAddressPool getInstance() {

return instance;

}

**/

 

 

// 内部静态类方式

private static class SingletonHolder {

private static final ImAddressPool INSTANCE = new ImAddressPool();  

}  

private ImAddressPool (){

init();

}

 

public static final ImAddressPool getInstance() {  

return SingletonHolder.INSTANCE;  

}  

原文地址:https://www.cnblogs.com/hujihon/p/4834624.html