All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hankcs.hanlp.utility.GlobalObjectPool Maven / Gradle / Ivy

There is a newer version: portable-1.8.5
Show newest version
/*
 * 
 * Hankcs
 * [email protected]
 * 2016-09-07 AM11:49
 *
 * 
 * Copyright (c) 2008-2016, 码农场. All Right Reserved, http://www.hankcs.com/
 * This source is subject to Hankcs. Please contact Hankcs to get more information.
 * 
 */
package com.hankcs.hanlp.utility;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Map;

/**
 * 全局对象缓存池
* 用于储存那些体积庞当的模型,如果该模型已经被加载过一次,那么就不需要重新加载。同时,如果JVM内存不够,并且没有任何强引用时,允许垃圾 * 回收器回收这些模型。 * * @author hankcs */ @SuppressWarnings("unchecked") public class GlobalObjectPool { /** * 缓存池 */ private static Map pool = new HashMap(); /** * 获取对象 * @param id 对象的id,可以是任何全局唯一的标示符 * @param 对象类型 * @return 对象 */ public synchronized static T get(Object id) { SoftReference reference = pool.get(id); if (reference == null) return null; return (T) reference.get(); } /** * 存放全局变量 * @param id * @param * @return */ public synchronized static T put(Object id, T value) { SoftReference old = pool.put(id, new SoftReference(value)); return old == null ? null : (T) old.get(); } /** * 清空全局变量 */ public synchronized static void clear() { pool.clear(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy