Skip to content

Commit 28e05ef

Browse files
committed
auto commit
1 parent 0bdaffd commit 28e05ef

File tree

2 files changed

+58
-43
lines changed

2 files changed

+58
-43
lines changed

notes/Java 容器.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* [HashMap](#hashmap)
1414
* [ConcurrentHashMap](#concurrenthashmap)
1515
* [LinkedHashMap](#linkedhashmap)
16+
* [WeekHashMap](#weekhashmap)
1617
* [参考资料](#参考资料)
1718
<!-- GFM-TOC -->
1819

@@ -982,6 +983,63 @@ public static void main(String[] args) {
982983
[3, 1, 4]
983984
```
984985

986+
## WeekHashMap
987+
988+
### 存储结构
989+
990+
WeakHashMap 的 Entry 继承自 WeakReference,被 WeakReference 关联的对象在下一次垃圾回收时会被回收。
991+
992+
WeakHashMap 主要用来实现缓存,通过使用 WeakHashMap 来引用缓存对象,由 JVM 对这部分缓存进行回收。
993+
994+
```java
995+
private static class Entry<K,V> extends WeakReference<Object> implements Map.Entry<K,V>
996+
```
997+
998+
### ConcurrentCache
999+
1000+
Tomcat 中的 ConcurrentCache 就使用了 WeakHashMap 来实现缓存功能。
1001+
1002+
ConcurrentCache 采取的是分代缓存:
1003+
1004+
- 经常使用的对象放入 eden 中,eden 使用 ConcurrentHashMap 实现,不用担心会被回收(伊甸园);
1005+
- 不常用的对象放入 longterm,longterm 使用 WeakHashMap 实现,用来存放比较老的对象,这些老对象会被垃圾收集器回收。
1006+
1007+
```java
1008+
public final class ConcurrentCache<K, V> {
1009+
1010+
private final int size;
1011+
1012+
private final Map<K, V> eden;
1013+
1014+
private final Map<K, V> longterm;
1015+
1016+
public ConcurrentCache(int size) {
1017+
this.size = size;
1018+
this.eden = new ConcurrentHashMap<>(size);
1019+
this.longterm = new WeakHashMap<>(size);
1020+
}
1021+
1022+
public V get(K k) {
1023+
V v = this.eden.get(k);
1024+
if (v == null) {
1025+
v = this.longterm.get(k);
1026+
if (v != null)
1027+
this.eden.put(k, v);
1028+
}
1029+
return v;
1030+
}
1031+
1032+
public void put(K k, V v) {
1033+
if (this.eden.size() >= size) {
1034+
this.longterm.putAll(this.eden);
1035+
this.eden.clear();
1036+
}
1037+
this.eden.put(k, v);
1038+
}
1039+
}
1040+
```
1041+
1042+
9851043
# 参考资料
9861044

9871045
- Eckel B. Java 编程思想 [M]. 机械工业出版社, 2002.

notes/Java 虚拟机.md

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -184,49 +184,6 @@ WeakReference<Object> wf = new WeakReference<Object>(obj);
184184
obj = null;
185185
```
186186

187-
WeakHashMap 的 Entry 继承自 WeakReference,主要用来实现缓存。
188-
189-
```java
190-
private static class Entry<K,V> extends WeakReference<Object> implements Map.Entry<K,V>
191-
```
192-
193-
Tomcat 中的 ConcurrentCache 就使用了 WeakHashMap 来实现缓存功能。ConcurrentCache 采取的是分代缓存,经常使用的对象放入 eden 中,而不常用的对象放入 longterm。eden 使用 ConcurrentHashMap 实现,longterm 使用 WeakHashMap,保证了不常使用的对象容易被回收。
194-
195-
```java
196-
public final class ConcurrentCache<K, V> {
197-
198-
private final int size;
199-
200-
private final Map<K, V> eden;
201-
202-
private final Map<K, V> longterm;
203-
204-
public ConcurrentCache(int size) {
205-
this.size = size;
206-
this.eden = new ConcurrentHashMap<>(size);
207-
this.longterm = new WeakHashMap<>(size);
208-
}
209-
210-
public V get(K k) {
211-
V v = this.eden.get(k);
212-
if (v == null) {
213-
v = this.longterm.get(k);
214-
if (v != null)
215-
this.eden.put(k, v);
216-
}
217-
return v;
218-
}
219-
220-
public void put(K k, V v) {
221-
if (this.eden.size() >= size) {
222-
this.longterm.putAll(this.eden);
223-
this.eden.clear();
224-
}
225-
this.eden.put(k, v);
226-
}
227-
}
228-
```
229-
230187
**(四)虚引用**
231188

232189
又称为幽灵引用或者幻影引用。一个对象是否有虚引用的存在,完全不会对其生存时间构成影响,也无法通过虚引用取得一个对象实例。

0 commit comments

Comments
 (0)