是“ConcurrentHashMap.putAll(...)”吗?原子?
ConcurrentHashMap.putAll(Map) 方法应该是原子的吗?
我在文档中找不到它,并且 ConcurrentMap 接口中也没有提到它,所以我猜答案是否定的。我要求它确定,因为说实话,如果该操作不是原子的,那么就没有意义。
如果它不是原子的,那么支持多个项目的原子插入的最佳方法是什么?回到原来的同步模式吗?
Is the method ConcurrentHashMap.putAll(Map) supposed to be atomic?
I cannot find it in the documentation and it is not mentioned in the ConcurrentMap interface, so I guess the answer is no. I am asking it to be sure, since it wouldn't make sense if that operation wasn't atomic to be honest.
If it isn't atomic, what would be the best way to support atomic inserts of multiple items? Back to the good old synchronized?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它不是原子的,不是。根据类文档:
要原子化它,您必须使用
synchronized
,是的。没有非阻塞的方法可以做到这一点。It's not atomic, no. According to the class documentation:
To atomicize it, you'll have to use
synchronized
, yes. There's no non-blocking way to do this.在文档的顶部
at the top of the doc
putAll()
不是原子的,而只是保证每个单独的put()
都是原子的。putAll()
is not atomic, but merely has the guarantee that each individualput()
is atomic.不仅如此:您必须在每个公共映射方法周围放置一个同步块,从而降低并发性。
Not only that: you have to put a synchronized block around every public map method thus degrading concurrency.