是“ConcurrentHashMap.putAll(...)”吗?原子?

发布于 2024-12-21 01:56:16 字数 187 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

多像笑话 2024-12-28 01:56:16

它不是原子的,不是。根据类文档

对于诸如 putAllclear 之类的聚合操作,并发检索可能反映仅插入或删除某些条目。

要原子化它,您必须使用synchronized,是的。没有非阻塞的方法可以做到这一点。

It's not atomic, no. According to the class documentation:

For aggregate operations such as putAll and clear, concurrent retrievals may reflect insertion or removal of only some entries.

To atomicize it, you'll have to use synchronized, yes. There's no non-blocking way to do this.

爱的那么颓废 2024-12-28 01:56:16

文档的顶部

对于诸如putAllclear之类的聚合操作,并发
检索可能反映仅插入或删除某些条目。

at the top of the doc

For aggregate operations such as putAll and clear, concurrent
retrievals may reflect insertion or removal of only some entries.

零時差 2024-12-28 01:56:16

putAll() 不是原子的,而只是保证每个单独的 put() 都是原子的。

putAll() is not atomic, but merely has the guarantee that each individual put() is atomic.

吝吻 2024-12-28 01:56:16

要原子化它,你必须使用同步,是的

不仅如此:您必须在每个公共映射方法周围放置一个同步块,从而降低并发性。

To atomicize it, you'll have to use synchronized, yes

Not only that: you have to put a synchronized block around every public map method thus degrading concurrency.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文