mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
1.修复BUG key为弱引用 value为强引用 会导致key无法被回收 弱引用失效
This commit is contained in:
parent
dacf9c80bb
commit
7152b5b21f
@ -2,6 +2,8 @@ package cn.hutool.core.lang.intern;
|
||||
|
||||
import cn.hutool.core.map.WeakConcurrentMap;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
/**
|
||||
* 使用WeakHashMap(线程安全)存储对象的规范化对象,注意此对象需单例使用!<br>
|
||||
*
|
||||
@ -10,13 +12,16 @@ import cn.hutool.core.map.WeakConcurrentMap;
|
||||
*/
|
||||
public class WeakInterner<T> implements Interner<T>{
|
||||
|
||||
private final WeakConcurrentMap<T, T> cache = new WeakConcurrentMap<>();
|
||||
private final WeakConcurrentMap<T, WeakReference<T>> cache = new WeakConcurrentMap<>();
|
||||
|
||||
@Override
|
||||
public T intern(T sample) {
|
||||
if(null == sample){
|
||||
if (sample == null) {
|
||||
return null;
|
||||
}
|
||||
return cache.computeIfAbsent(sample, (key)->sample);
|
||||
T val;
|
||||
do {
|
||||
val = this.cache.computeIfAbsent(sample, WeakReference::new).get();
|
||||
} while (val == null);
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user