-
Notifications
You must be signed in to change notification settings - Fork 46k
Open
Labels
questionFurther information is requestedFurther information is requested
Description
// 线程 A
map.putIfAbsent(key, value);
// 线程 B
map.putIfAbsent(key, anotherValue);
eg:
public static void main(String[] args) {
Map<String, Integer> map = new ConcurrentHashMap<>();
// 初始时 map 为空
// 计算键 "key1" 对应的值,如果不存在则根据计算逻辑生成值
Integer value1 = map.putIfAbsent("key1", 1);
System.out.println("map = " + map); // 输出: map = {key1=1}
System.out.println("value1 = " + value1); // 输出: value1 = 1
// 再次计算键 "key1" 对应的值,由于键已存在,直接返回已有的值
Integer value2 = map.putIfAbsent("key1", 2);
System.out.println("map = " + map); // 输出: map = {key1=1}
System.out.println("value2 = " + value2); // 输出: value2 = 1
}
以上操作也并不会实现预期的 (key, anotherValue)。
Metadata
Metadata
Assignees
Labels
questionFurther information is requestedFurther information is requested
Projects
Milestone
Relationships
Development
Select code repository
Activity
Snailclimb commentedon May 8, 2025
putIfAbsent
的核心在于“如果不存在则放入”,它的返回值是操作前与 key 关联的值 (如果不存在则为 null)。computeIfAbsent
的核心在于“如果不存在则计算并放入”,它的返回值是操作后与 key 关联的值 (无论是新计算的还是原有的)。你的代码示例本身是
putIfAbsent
的正确用法,但你对putIfAbsent
返回值的理解可能与其实际行为略有出入,特别是当 key 首次被放入时。putIfAbsent
的确实现了“如果不存在则放入一个初始值”的原子操作,并且在并发环境下,只有一个线程的putIfAbsent
会成功地将初始值放入(并返回 null),后续线程调用putIfAbsent
则会获取到已存在的值。