Skip to content

【复合操作的非原子性】以下ConcurrentHashMap操作也不会实现预期的anotherValue #2668

@GuZyx

Description

@GuZyx

// 线程 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)。

Image

Activity

Snailclimb

Snailclimb commented on May 8, 2025

@Snailclimb
Owner

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
  • putIfAbsent 的核心在于“如果不存在则放入”,它的返回值是操作前与 key 关联的值 (如果不存在则为 null)。
  • computeIfAbsent 的核心在于“如果不存在则计算并放入”,它的返回值是操作后与 key 关联的值 (无论是新计算的还是原有的)。
public static void main(String[] args) {
    Map<String, Integer> map = new ConcurrentHashMap<>();

    // 初始时 map 为空
    // 第一次调用 putIfAbsent
    Integer value1 = map.putIfAbsent("key1", 1);
    System.out.println("map = " + map); // map = {key1=1}
    System.out.println("value1 = " + value1); // value1 = null  <-- 这里是关键!
                                              // 因为 "key1" 之前不存在,所以放入了 1,返回了 null。

    // 第二次调用 putIfAbsent
    Integer value2 = map.putIfAbsent("key1", 2); // "key1" 已经存在,值为 1
    System.out.println("map = " + map); // map = {key1=1} (没有变化)
    System.out.println("value2 = " + value2); // value2 = 1   <-- 返回的是 "key1" 当前关联的值,即 1。
                                              // 并没有放入 2。
}

你的代码示例本身是 putIfAbsent 的正确用法,但你对 putIfAbsent 返回值的理解可能与其实际行为略有出入,特别是当 key 首次被放入时。putIfAbsent 的确实现了“如果不存在则放入一个初始值”的原子操作,并且在并发环境下,只有一个线程的 putIfAbsent 会成功地将初始值放入(并返回 null),后续线程调用 putIfAbsent 则会获取到已存在的值。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @Snailclimb@GuZyx

        Issue actions

          【复合操作的非原子性】以下ConcurrentHashMap操作也不会实现预期的anotherValue · Issue #2668 · Snailclimb/JavaGuide