diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/lang/ref/ReferenceUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/lang/ref/ReferenceUtil.java index b54fd0c8f..7aecaa00f 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/lang/ref/ReferenceUtil.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/lang/ref/ReferenceUtil.java @@ -81,7 +81,7 @@ public class ReferenceUtil { } /** - * {@code null}全的解包获取原始对象 + * {@code null}安全的解包获取原始对象 * * @param 对象类型 * @param obj Ref对象 diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/map/reference/WeakConcurrentMapTest.java b/hutool-core/src/test/java/org/dromara/hutool/core/map/reference/WeakConcurrentMapTest.java new file mode 100644 index 000000000..49bedd841 --- /dev/null +++ b/hutool-core/src/test/java/org/dromara/hutool/core/map/reference/WeakConcurrentMapTest.java @@ -0,0 +1,48 @@ +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 static org.junit.jupiter.api.Assertions.*; + +public class WeakConcurrentMapTest { + @Test + public void testWeakConcurrentMap() { + final WeakConcurrentMap map = new WeakConcurrentMap<>(); + + // Test if the map is initially empty + assertTrue(map.isEmpty()); + + // Test if the map can store and retrieve values correctly + map.put("key1", "value1"); + assertEquals("value1", map.get("key1")); + + map.computeIfAbsent("key2", key -> "value2"); + // Test if the map can handle concurrent modifications + new Thread(() -> { + map.put("key2", "value2"); + }).start(); + + // Test if the map has correctly stored the value from the concurrent modification + assertEquals("value2", map.get("key2")); + + assertTrue(map.containsKey("key1")); + assertTrue(map.containsKey("key2")); + } + + @Test + void computeIfAbsentTest() { + final WeakConcurrentMap map = new WeakConcurrentMap<>(); + + for (int i = 0; i < 1000; i++) { + ThreadUtil.execute(()->{ + final String s = map.computeIfAbsent(RandomUtil.randomString(1), key -> "value1"); + assertEquals("value1", s); + }); + } + + ThreadUtil.sleep(500); + assertFalse(map.isEmpty()); + } +}