We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
// 线程 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)。
The text was updated successfully, but these errors were encountered:
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
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
computeIfAbsent
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 则会获取到已存在的值。
Sorry, something went wrong.
No branches or pull requests
// 线程 A
map.putIfAbsent(key, value);
// 线程 B
map.putIfAbsent(key, anotherValue);
eg:
以上操作也并不会实现预期的 (key, anotherValue)。
The text was updated successfully, but these errors were encountered: