标签:toe number 删除 设置 返回 接下来 mis sof cti
另外。当我们将accessOrder设置为true时。能够使遍历顺序和訪问顺序一致,其
内部双向链表将会依照最近最少訪问到最近最多訪问的顺序排列Entry对象,这能够用来做缓存。 private final LinkedHashMap<K, V> map;
/** Size of this cache in units. Not necessarily the number of elements. */
private int size;//当前大小
private int maxSize;//最大容量
private int putCount;//put次数
private int createCount;//create次数
private int evictionCount;//回收次数
private int hitCount;//命中次数
private int missCount;//丢失次数LinkedHashMap的初始化放在构造器中:public LruCache(int maxSize) {
if (maxSize <= 0) {
throw new IllegalArgumentException("maxSize <= 0");
}
this.maxSize = maxSize;
this.map = new LinkedHashMap<K, V>(0, 0.75f, true);
}首先是put方法:
public final V put(K key, V value) {
if (key == null || value == null) {//键值不同意为空
throw new NullPointerException("key == null || value == null");
}
V previous;
synchronized (this) {//线程安全
putCount++;
size += safeSizeOf(key, value);
previous = map.put(key, value);
if (previous != null) {//之前已经插入过同样的key
size -= safeSizeOf(key, previous);//那么减去该entry的容量,由于发生覆盖
}
}
if (previous != null) {
entryRemoved(false, key, previous, value);//这种方法默认空实现
}
trimToSize(maxSize);//若容量超过maxsize,将会删除近期非常少訪问的entry
return previous;
}
除此之外,put操作被加锁了,所以是线程安全的!
public void trimToSize(int maxSize) {
while (true) {//不断删除linkedHashMap头部entry,也就是近期最少訪问的条目。直到size小于最大容量
K key;
V value;
synchronized (this) {//线程安全
if (size < 0 || (map.isEmpty() && size != 0)) {
throw new IllegalStateException(getClass().getName()
+ ".sizeOf() is reporting inconsistent results!");
}
if (size <= maxSize || map.isEmpty()) {//直到容量小于最大容量为止
break;
}
Map.Entry<K, V> toEvict = map.entrySet().iterator().next();//指向链表头
key = toEvict.getKey();
value = toEvict.getValue();
map.remove(key);//删除最少訪问的entry
size -= safeSizeOf(key, value);
evictionCount++;
}
entryRemoved(true, key, value, null);
}
}private void trimToSize(int maxSize) {
while (true) {
K key;
V value;
synchronized (this) {
if (size < 0 || (map.isEmpty() && size != 0)) {
throw new IllegalStateException(getClass().getName()
+ ".sizeOf() is reporting inconsistent results!");
}
if (size <= maxSize) {
break;
}
Map.Entry<K, V> toEvict = null;
for (Map.Entry<K, V> entry : map.entrySet()) {
toEvict = entry;
}
if (toEvict == null) {
break;
}
key = toEvict.getKey();
value = toEvict.getValue();
map.remove(key);
size -= safeSizeOf(key, value);
evictionCount++;
}
entryRemoved(true, key, value, null);
}
}
public final V get(K key) {
if (key == null) {//不同意空键
throw new NullPointerException("key == null");
}
V mapValue;
synchronized (this) {//线程安全
mapValue = map.get(key);//调用LinkedHashMap的get方法
if (mapValue != null) {
hitCount++;//命中次数加1
return mapValue;//返回value
}
missCount++;//未命中
}
V createdValue = create(key);//默认返回为false
if (createdValue == null) {
return null;
}
synchronized (this) {
createCount++;//假设创建成功,那么create次数加1
mapValue = map.put(key, createdValue);//放到哈希表中
if (mapValue != null) {
// There was a conflict so undo that last put
map.put(key, mapValue);
} else {
size += safeSizeOf(key, createdValue);
}
}
if (mapValue != null) {
entryRemoved(false, key, createdValue, mapValue);
return mapValue;
} else {
trimToSize(maxSize);
return createdValue;
}
} public final V remove(K key) {
if (key == null) {
throw new NullPointerException("key == null");
}
V previous;
synchronized (this) {
previous = map.remove(key);//调用LinkedHashMap的remove方法
if (previous != null) {
size -= safeSizeOf(key, previous);
}
}
if (previous != null) {
entryRemoved(false, key, previous, null);
}
return previous;//返回value
} protected int sizeOf(K key, V value) {//用于计算每一个条目的大小
return 1;
}snapshot方法,返回当前缓存中全部的条目集合 public synchronized final Map<K, V> snapshot() {
return new LinkedHashMap<K, V>(map);
}标签:toe number 删除 设置 返回 接下来 mis sof cti
原文地址:http://www.cnblogs.com/lytwajue/p/7202120.html