auto commit

This commit is contained in:
CyC2018
2018-07-27 00:49:18 +08:00
parent 0bdaffd59c
commit 28e05ef54f
2 changed files with 58 additions and 43 deletions

View File

@ -184,49 +184,6 @@ WeakReference<Object> wf = new WeakReference<Object>(obj);
obj = null;
```
WeakHashMap 的 Entry 继承自 WeakReference主要用来实现缓存。
```java
private static class Entry<K,V> extends WeakReference<Object> implements Map.Entry<K,V>
```
Tomcat 中的 ConcurrentCache 就使用了 WeakHashMap 来实现缓存功能。ConcurrentCache 采取的是分代缓存,经常使用的对象放入 eden 中,而不常用的对象放入 longterm。eden 使用 ConcurrentHashMap 实现longterm 使用 WeakHashMap保证了不常使用的对象容易被回收。
```java
public final class ConcurrentCache<K, V> {
private final int size;
private final Map<K, V> eden;
private final Map<K, V> longterm;
public ConcurrentCache(int size) {
this.size = size;
this.eden = new ConcurrentHashMap<>(size);
this.longterm = new WeakHashMap<>(size);
}
public V get(K k) {
V v = this.eden.get(k);
if (v == null) {
v = this.longterm.get(k);
if (v != null)
this.eden.put(k, v);
}
return v;
}
public void put(K k, V v) {
if (this.eden.size() >= size) {
this.longterm.putAll(this.eden);
this.eden.clear();
}
this.eden.put(k, v);
}
}
```
**(四)虚引用**
又称为幽灵引用或者幻影引用。一个对象是否有虚引用的存在,完全不会对其生存时间构成影响,也无法通过虚引用取得一个对象实例。