public class SingletonFactory {
@SuppressWarnings("rawtypes")
private static Map<Class,Object> instaces = new ConcurrentHashMap<Class, Object>();
@SuppressWarnings("rawtypes")
private static Map<Class,WeakReference<Object>> weakReferenceInstaces = new ConcurrentHashMap<Class, WeakReference<Object>>();
/**
* 创建可不被回收的单例模式,当没有对象引用,单例对象将被gc掉
* @param className
* @return
* @throws InstantiationException
* @throws IllegalAccessException
*/
@SuppressWarnings("unchecked")
public static <E> E getInstace(Class<E> className){
Object instace =instaces.get(className);
if(instace==null){
synchronized (SingletonFactory.class) {
instace =instaces.get(className);
if(instace==null){
try {
instace = className.newInstance();
} catch (InstantiationException e) {
e.printStackTr
单例工厂SingletonFactory
最新推荐文章于 2022-12-05 14:44:05 发布