📄 weakreferencecache.java
字号:
// You can redistribute this software and/or modify it under the terms of// the Ozone Core License version 1 published by ozone-db.org.//// Copyright (C) 2003-@year@, Leo Mekenkamp. All rights reserved.//// $Id: WeakReferenceCache.java,v 1.3 2004/03/21 21:05:51 leomekenkamp Exp $package org.ozoneDB.core.storage;import java.lang.ref.ReferenceQueue;import java.lang.ref.WeakReference;import java.util.Properties;/** * <p>Caches objects which may be garbage collected at any time by the Java virtual * machine garbage collector. Uses weak references to hold objects in memory.</p> * * @author <a href="mailto:leoATmekenkampD0Tcom">Leo Mekenkamp (mind the anti sp@m)</a> * @version $Id: WeakReferenceCache.java,v 1.3 2004/03/21 21:05:51 leomekenkamp Exp $ */public class WeakReferenceCache extends AbstractReferenceCache { private static class ObjectReference extends WeakReference implements KeyedReference { /** * same key as the key passed in <code>put(key, value)</code> */ private Object key; ObjectReference(Object key, Object value, ReferenceQueue q) { super(value, q); this.key = key; } public Object getKey() { return key; } public String toString() { return key.toString(); } public int hashCode() { return key.hashCode(); } public boolean equals(Object o) { if (!(o instanceof ObjectReference)) { return false; } else { ObjectReference otherRef = (ObjectReference) o; return otherRef.getKey().equals(getKey()); } } } // ObjectReference public WeakReferenceCache(Properties properties, String prefix) { super(properties, prefix); } protected KeyedReference createKeyedReference(Object key, Object value, ReferenceQueue referenceQueue) { return new ObjectReference(key, value, referenceQueue); } // DEBUG AND BUGTEST CODE ONLY FOLLOWING; not needed for normal operation public WeakReferenceCache() { } private static class Item { public Object key; public byte[] filling = new byte[1000]; Item(Object key) { this.key = key; } public String toString() { return key.toString(); } } public static void main(String args[]) { WeakReferenceCache cache = new WeakReferenceCache(); int last = 0; for (int i = 0; i < 1000000; i++) { Object key = new Integer(i); Item item = new Item(key); cache.put(key, item);// for (int j = 0; j < 20; j++) {// cache.get(new Integer(j));// } if (cache.size() < last) { System.out.println(cache.size() + ", last: " + last); } last = cache.size(); } System.out.println("completed successfully"); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -