周 祥,北邮-从虚拟机视角谈 Java 应用性
public class ObjectFactory {
/** A counter for counting the number of objects in use. */
private static int objInUse = 0;
/** A counter for counting the number of objects in pool. */
private static int objInPool = 0;
/** The object pool. */
private static HashMap objectPool = new HashMap();
/** The corresponding object pool for a specific class. */
private static LinkedHashSet subObjPool;
/** Generate object for use */
public synchronized static Object generate(String className) {
Object retObj = null;
subObjPool = (LinkedHashSet) objectPool.get(className);
if (subObjPool != null && subObjPool.size() < 0) {
retObj = subObjPool.iterator().next();
subObjPool.remove(retObj);
objInPool--;
} else {
try {
retObj = newObj(className);
} catch (InstantiationException ie) {
return null;
} catch (IllegalAccessException iae) {
return null;
} catch (ClassNotFoundException cnfe) {
return null;
}
}
objInUse++;
return retObj;
}
public synchronized static void drop(Object freeObject) {
if (freeObject != null) {
subObjPool = (LinkedHashSet) objectPool.get(className);
if (subObjPool == null) {
subObjPool = new LinkedHashSet(); objectPool.put(className, subObjPool);
为了应用于消费类电子设备的开发; 1994年Internet开始起步,1995年NetScape宣布把 1994年Internet开始起步 1995年NetScape宣布把 Java集成到其浏览器中,可执行的Java...
为了应用于消费类电子设备的开发; 1994年Internet开始起步,1995年NetScape宣布把 1994年Internet开始起步 1995年NetScape宣布把 Java集成到其浏览器中,可执行的Java...
热门文档