import java.util.*;
public class LRUCacheWithListener {
class CacheNode {
CacheNode prev;
CacheNode next;
Abandon value;
Object key;
public CacheNode() {
}
}
private int cacheSize;
private HashMap nodes;
private int currentSize;
private CacheNode first;
private CacheNode last;
private static int DEFAULT_SIZE = 10;
public LRUCacheWithListener() {
this(DEFAULT_SIZE);
}
public LRUCacheWithListener(int poolSize) {
cacheSize = poolSize;
currentSize = 0;
first = null; //
last = null; //
nodes = new HashMap(poolSize);
}
public synchronized Object get(Object key) {
CacheNode node = (CacheNode) nodes.get(key);
if (node != null) {
moveToHead(node);
return node.value;
}
else {
return null;
}
}
private void moveToHead(CacheNode node) {
if (node == first) {
return;
}
if (node.prev != null) {
node.prev.next = node.next;
}
if (node.next != null) {
node.next.prev = node.prev;
}
if (last == node) {
last = node.prev;
}
if (first != null) {
node.next = first;
first.prev = node;
}
first = node;
node.prev = null;
if (last == null) {
last = first;
}
}
public synchronized Object remove(Object key) {
CacheNode node = (CacheNode) nodes.get(key);
if (node != null) {
if (node.prev != null) {
node.prev.next = node.next;
}
if (node.next != null) {
node.next.prev = node.prev;
}
if (last == node) {
last = node.prev;
}
if (first == node) {
first = node.next;
}
}
return node;
}
public synchronized void put(Object key, Abandon value) {
CacheNode node = (CacheNode) nodes.get(key);
if (node == null) {
//池满,删除最久没有使用的对象
if (currentSize >= cacheSize) {
if (last != null) {
nodes.remove(last.key);
}
removeLast();
}
//池没有满,直接把对象放入池中
else {
currentSize++;
}
node = getANewCacheNode();
}
node.value = value;
node.key = key;
// 把放入池的这个对象移动到链表的头部,表示最近最短被使用过
moveToHead(node);
nodes.put(key, node);
}
public synchronized void clear() {
if (first!=null){
Iterator i= nodes.values().iterator();
//触发事件,该池已经被清空
CacheNode n;
while(i.hasNext()){
n = (CacheNode)(i.next());
n.value.poolClear();
}
}
first = null;
last = null;
}
private CacheNode getANewCacheNode(){
CacheNode node = new CacheNode();
return node;
}
private void removeLast() {
if (last != null) {
//对象从池中被抛弃,触发事件
last.value.onAbandon();
if (last.prev != null) {
last.prev.next = null;
}
else {
first = null;
}
last = last.prev;
}
}
}
******代码2
package memorymanager.lrucache;
public interface Abandon {
//
public void onAbandon();
public void poolClear();
}
代码3,测试部分
package memorymanager.lrucache;
public class CacheNodeWithListener implements Abandon {
int id;
public CacheNodeWithListener() {
}
public CacheNodeWithListener(int i) {
id=i;
}
public void onAbandon(){
System.out.println(this+"---onAbandon()");
}
public void poolClear(){
System.out.println(this+"---poolClear()");
}
public String toString(){
return "id="+id ;
}
static public void main(String [] s){
LRUCacheWithListener pool =new LRUCacheWithListener(3);
int i;
for (i=1;i<=5;i++) {
pool.put("obj"+i,new CacheNodeWithListener(i));
}
pool.clear();
}
}
原文:http://www.aptime.cn/forum/ShowPost.asp?menu=Previous&ForumID=26&ThreadID=3107
posted on 2006-10-10 12:55 hopeshared 阅读(1181) 评论(0) 编辑 收藏 所属分类: Java