- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
ConcurrentHashMap#computeIfAbsent
中的 Javadoc 说
The computation should be short and simple, and must not attempt to update any other mappings of this map.
但是,据我所知,在 mappingFunction
中使用 remove()
和 clear()
方法效果很好。比如这个
Key element = elements.computeIfAbsent(key, e -> {
if (usages.size() == maxSize) {
elements.remove(oldest);
}
return loader.load(key);
});
在 mappingFunction
中使用 remove() 方法会带来什么不良后果?
最佳答案
这是一个不良后果的例子:
ConcurrentHashMap<Integer,String> cmap = new ConcurrentHashMap<> ();
cmap.computeIfAbsent (1, e-> {cmap.remove (1); return "x";});
此代码导致死锁。
关于java - 在 ConcurrentHashMap#computeIfAbsent 中更新其他键的后果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44224952/
我有一个 Hashmap,在其中维护不同类型的读取器到它们各自的 java 类实现的映射。我有一个多线程 Java 服务器,支持 32 种类型的阅读器。 您可以假设每 30 秒,每种类型的读取器都会有
Java 8 中引入了一个新的 computeIfAbsent API。ConcurrentHashMap's impelementation of it 的 javadocs状态: If the s
在本文中,我们将学习 Java ConcurrentHashMap 类的 computeIfAbsent() 方法。找到 computeIfAbsent 方法的 Java 文档。 V computeI
computeIfAbsent 是 java.util.Map 的默认方法,已在 Java 8 中引入。当与指定键关联的值不可用或为 null 时,computeIfAbsent 方法起作用,在这种情
我有这样的代码: ConcurrentMap map = new ConcurrentHashMap<>(); AtomicBoolean isNew = new AtomicBoolean(fals
java.util.HashMap 的文档明确指出:“如果多个线程同时访问一个 HashMap ,并且至少有一个线程在结构上修改了该映射,则它必须在外部进行同步。” 但是,请考虑使用映射作为缓存以减少
升级到 Android Studio 3 Canary 4 后出现此错误: Gradle sync failed: Argument for @NotNull parameter 'key' of c
考虑以下某种固定大小缓存的实现,它允许通过整数句柄进行查找: static class HandleCache { private final AtomicInteger counter =
我是 Java 的新手,有点从 C# 过渡到 Java。 java.util.function有一个定义为 Function 的接口(interface)这是输入到 computeIfAbsent M
于是我对 Java 的 Map computeIfAbsent 产生了好奇。 (使用 java8)方法,我希望有人能告诉我为什么会这样,因为我无法真正理解该问题背后的逻辑。 所以,我有一个带有键的映射
我很想用Map.computeIfAbsent但是距离本科生的lambda已经太久了。 几乎直接来自文档:它提供了一个旧方法的示例: Map whoLetDogsOut = new Concurren
我正在尝试制作具有以下功能的工厂: 它应该总是返回一个 Filter 对象。 从 hashmap = 如果字符串字母(键)已经存在于 hashmap 中,它应该只从 hashmap 中获取它的值(对象
我有以下代码,它是一个玩具代码,但可以重现该问题: import java.util.*; import java.util.concurrent.ConcurrentHashMap; import
ConcurrentHashMap#computeIfAbsent 中的 Javadoc 说 The computation should be short and simple, and must
整个方法调用是原子的还是只是 BiFunction 执行的原子?是阻塞所有键还是只阻塞同一键上的调用? 最佳答案 以下详细信息适用于 OpenJDK Java 11。 这三种方法持有 Node 的锁在
我正在练习 Cay S. Horstmann 所著的“Java SE 8 for the really Impatient”一书的练习。有 2 个练习要求相同算法的不同实现,一个使用 merge ,其
我正在使用 ConcurrentHashMap在 Scala 中,我想使用 computeIfAbsent()方法,但无法弄清楚第二个参数的语法。有人可以告诉我什么是正确的语法吗? 运行以下代码时 v
有没有办法模拟ConcurrentHashmap.computeIfAbsent但不将条目分配给hashmap。我只需要当 HashMap 中的条目尚不存在时该方法生成的实例。这些都在线程中运行。所以
我有一组非常适合我的代码: for (String word : distinctWordsInOneLigne) { Map> map = new H
大家好,我想知道如何在不使用 lambda 表达式的情况下使用 JSONObject.computeIfAbsent 。实际上,这是我的代码 list.computeIfAbsent(id, k ->
我是一名优秀的程序员,十分优秀!