mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix #IA5GMH
This commit is contained in:
parent
d0fdf42dcd
commit
be11b05a6f
@ -12,9 +12,10 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.core.bean;
|
package org.dromara.hutool.core.bean;
|
||||||
|
|
||||||
import org.dromara.hutool.core.func.SerSupplier;
|
|
||||||
import org.dromara.hutool.core.map.reference.WeakConcurrentMap;
|
import org.dromara.hutool.core.map.reference.WeakConcurrentMap;
|
||||||
|
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bean属性缓存<br>
|
* Bean属性缓存<br>
|
||||||
* 缓存用于防止多次反射造成的性能问题
|
* 缓存用于防止多次反射造成的性能问题
|
||||||
@ -27,18 +28,20 @@ public enum BeanDescCache {
|
|||||||
*/
|
*/
|
||||||
INSTANCE;
|
INSTANCE;
|
||||||
|
|
||||||
private final WeakConcurrentMap<Class<?>, StrictBeanDesc> bdCache = new WeakConcurrentMap<>();
|
private final WeakConcurrentMap<Class<?>, BeanDesc> bdCache = new WeakConcurrentMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得属性名和{@link StrictBeanDesc}Map映射
|
* 获得属性名和{@link BeanDesc}Map映射
|
||||||
*
|
*
|
||||||
* @param beanClass Bean的类
|
* @param beanClass Bean的类
|
||||||
* @param supplier 对象不存在时创建对象的函数
|
* @param supplier 对象不存在时创建对象的函数
|
||||||
* @return 属性名和 {@link StrictBeanDesc}映射
|
* @param <T> BeanDesc子类
|
||||||
|
* @return 属性名和 {@link BeanDesc}映射
|
||||||
* @since 5.4.2
|
* @since 5.4.2
|
||||||
*/
|
*/
|
||||||
public StrictBeanDesc getBeanDesc(final Class<?> beanClass, final SerSupplier<StrictBeanDesc> supplier) {
|
@SuppressWarnings("unchecked")
|
||||||
return bdCache.computeIfAbsent(beanClass, (key) -> supplier.get());
|
public <T extends BeanDesc> T getBeanDesc(final Class<?> beanClass, final Supplier<T> supplier) {
|
||||||
|
return (T) bdCache.computeIfAbsent(beanClass, (key) -> supplier.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -136,18 +136,40 @@ public abstract class ReferenceConcurrentMap<K, V> implements ConcurrentMap<K, V
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public V computeIfAbsent(final K key, final Function<? super K, ? extends V> mappingFunction) {
|
public V computeIfAbsent(final K key, final Function<? super K, ? extends V> mappingFunction) {
|
||||||
|
V result = null;
|
||||||
|
while(null == result){
|
||||||
this.purgeStale();
|
this.purgeStale();
|
||||||
final Ref<V> vReference = this.raw.computeIfAbsent(wrapKey(key),
|
final Ref<V> vReference = this.raw.computeIfAbsent(wrapKey(key),
|
||||||
kReference -> wrapValue(mappingFunction.apply(unwrap(kReference))));
|
kReference -> wrapValue(mappingFunction.apply(unwrap(kReference))));
|
||||||
return unwrap(vReference);
|
|
||||||
|
// issue#IA5GMH 如果vReference在此时被GC回收,则unwrap后为null,需要循环计算
|
||||||
|
// 但是当用户提供的值本身为null,则直接返回之
|
||||||
|
if(NullRef.NULL == vReference){
|
||||||
|
// 用户提供的值本身为null
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
result = unwrap(vReference);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public V computeIfPresent(final K key, final BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
|
public V computeIfPresent(final K key, final BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
|
||||||
|
V result = null;
|
||||||
|
while(null == result){
|
||||||
this.purgeStale();
|
this.purgeStale();
|
||||||
final Ref<V> vReference = this.raw.computeIfPresent(wrapKey(key),
|
final Ref<V> vReference = this.raw.computeIfPresent(wrapKey(key),
|
||||||
(kReference, vReference1) -> wrapValue(remappingFunction.apply(unwrap(kReference), unwrap(vReference1))));
|
(kReference, vReference1) -> wrapValue(remappingFunction.apply(unwrap(kReference), unwrap(vReference1))));
|
||||||
return unwrap(vReference);
|
|
||||||
|
// issue#IA5GMH 如果vReference在此时被GC回收,则unwrap后为null,需要循环计算
|
||||||
|
// 但是当用户提供的值本身为null,则直接返回之
|
||||||
|
if(NullRef.NULL == vReference){
|
||||||
|
// 用户提供的值本身为null
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
result = unwrap(vReference);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -358,6 +380,9 @@ public abstract class ReferenceConcurrentMap<K, V> implements ConcurrentMap<K, V
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private Ref<V> wrapValue(final Object value) {
|
private Ref<V> wrapValue(final Object value) {
|
||||||
|
if(null == value){
|
||||||
|
return (Ref<V>) NullRef.NULL;
|
||||||
|
}
|
||||||
return wrapValue((V) value, this.lastValueQueue);
|
return wrapValue((V) value, this.lastValueQueue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -371,4 +396,14 @@ public abstract class ReferenceConcurrentMap<K, V> implements ConcurrentMap<K, V
|
|||||||
private static <T> T unwrap(final Ref<T> obj) {
|
private static <T> T unwrap(final Ref<T> obj) {
|
||||||
return ReferenceUtil.get(obj);
|
return ReferenceUtil.get(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
|
private static class NullRef implements Ref {
|
||||||
|
public static final Object NULL = new NullRef();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object get() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2024. looly(loolly@aliyun.com)
|
||||||
|
* Hutool is licensed under Mulan PSL v2.
|
||||||
|
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||||
|
* You may obtain a copy of Mulan PSL v2 at:
|
||||||
|
* https://license.coscl.org.cn/MulanPSL2
|
||||||
|
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||||
|
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||||
|
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||||
|
* See the Mulan PSL v2 for more details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.dromara.hutool.core.map.reference;
|
||||||
|
|
||||||
|
import org.dromara.hutool.core.lang.ref.Ref;
|
||||||
|
import org.dromara.hutool.core.lang.ref.StrongObj;
|
||||||
|
import org.dromara.hutool.core.lang.ref.WeakObj;
|
||||||
|
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
|
||||||
|
|
||||||
|
import java.lang.ref.ReferenceQueue;
|
||||||
|
import java.util.concurrent.ConcurrentMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 线程安全的WeakMap实现<br>
|
||||||
|
* 键为Weak引用,即,在GC时发现弱引用会回收其对象
|
||||||
|
*
|
||||||
|
* @param <K> 键类型
|
||||||
|
* @param <V> 值类型
|
||||||
|
* @author looly
|
||||||
|
* @since 6.0.0
|
||||||
|
*/
|
||||||
|
public class WeakKeyConcurrentMap<K, V> extends ReferenceConcurrentMap<K, V> {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造
|
||||||
|
*/
|
||||||
|
public WeakKeyConcurrentMap() {
|
||||||
|
this(new SafeConcurrentHashMap<>());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造
|
||||||
|
*
|
||||||
|
* @param raw {@link ConcurrentMap}实现
|
||||||
|
*/
|
||||||
|
public WeakKeyConcurrentMap(final ConcurrentMap<Ref<K>, Ref<V>> raw) {
|
||||||
|
super(raw);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
Ref<K> wrapKey(final K key, final ReferenceQueue<? super K> queue) {
|
||||||
|
return new WeakObj<>(key, queue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
Ref<V> wrapValue(final V value, final ReferenceQueue<? super V> queue) {
|
||||||
|
return new StrongObj<>(value);
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +1,12 @@
|
|||||||
package org.dromara.hutool.core.date;
|
package org.dromara.hutool.core.date;
|
||||||
|
|
||||||
import org.dromara.hutool.core.lang.Console;
|
import org.dromara.hutool.core.lang.Console;
|
||||||
|
import org.junit.jupiter.api.Disabled;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class IssueI8IUTBTest {
|
public class IssueI8IUTBTest {
|
||||||
@Test
|
@Test
|
||||||
|
@Disabled
|
||||||
void parseTest() {
|
void parseTest() {
|
||||||
final DateTime parse = DateUtil.parse("May 8, 2009 5:57:51 PM");
|
final DateTime parse = DateUtil.parse("May 8, 2009 5:57:51 PM");
|
||||||
Console.log(parse);
|
Console.log(parse);
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package org.dromara.hutool.core.map.reference;
|
package org.dromara.hutool.core.map.reference;
|
||||||
|
|
||||||
import org.dromara.hutool.core.thread.ThreadUtil;
|
|
||||||
import org.dromara.hutool.core.util.RandomUtil;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
@ -29,20 +27,21 @@ public class WeakConcurrentMapTest {
|
|||||||
|
|
||||||
assertTrue(map.containsKey("key1"));
|
assertTrue(map.containsKey("key1"));
|
||||||
assertTrue(map.containsKey("key2"));
|
assertTrue(map.containsKey("key2"));
|
||||||
|
|
||||||
|
// null值
|
||||||
|
String s = map.computeIfAbsent("key3", key -> null);
|
||||||
|
assertNull(s);
|
||||||
|
|
||||||
|
// 允许key为null
|
||||||
|
s = map.computeIfAbsent(null, key -> null);
|
||||||
|
assertNull(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("StringOperationCanBeSimplified")
|
||||||
@Test
|
@Test
|
||||||
void computeIfAbsentTest() {
|
void computeIfAbsentTest() {
|
||||||
final WeakConcurrentMap<String, String> map = new WeakConcurrentMap<>();
|
final WeakConcurrentMap<String, String> map = new WeakConcurrentMap<>();
|
||||||
|
final String value = map.computeIfAbsent("key1", key -> new String("value1"));
|
||||||
for (int i = 0; i < 1000; i++) {
|
assertNotNull(value);
|
||||||
ThreadUtil.execute(()->{
|
|
||||||
final String s = map.computeIfAbsent(RandomUtil.randomString(1), key -> "value1");
|
|
||||||
assertEquals("value1", s);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
ThreadUtil.sleep(500);
|
|
||||||
assertFalse(map.isEmpty());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user