java - ConcurrentHashMap 的 KeySet 迭代器是线程安全的吗?

标签 java multithreading

我只是想探索ThreadSafe 是什么意思?

以下是我的理解:

它看起来像我;允许多个线程同时访问一个集合;这与其同步无关。例如任何没有 synchronized 关键字的方法;是线程安全的,意味着多个线程可以访问它。

开发人员可以选择在此方法上维护更多逻辑(同步),以在多线程访问数据时保持数据完整性。这与线程安全是分开的。

如果我的上述陈述是错误的;只需阅读下面的 JAVA DOC for `ConcurrentHashMap:

keySet: The view's iterator is a "weakly consistent" iterator that will never throw ConcurrentModificationException, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.

上面的声明说 keySet 迭代器不会保证数据的完整性;而多线程正在修改集合。

你能回答我吗,*ConcurrentHashMap 的 KeySet 迭代器是线程安全的吗?

我对线程安全的理解是正确的??

最佳答案

keySet: The view's iterator is a "weakly consistent" iterator that will never throw ConcurrentModificationException, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction

这本身就说明了,ConcurrentHashMap 的 KeySet 迭代器是线程安全的。

关于java - ConcurrentHashMap 的 KeySet 迭代器是线程安全的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19584378/

相关文章:

java - 关于 "+"运算符及其 StringBuilder 实现

java - 随机长整型始终是最小值

java - 单例实现接口(interface)的优势

java - 使用线程插入mysql

java - 线程启动方法需要什么?为什么不只有运行方法?

java - 如何在 zuul/ribbon 中获取请求将转发到的服务器的主机名

java sql SQLException : Parameter index out of range (1 > number of parameters, 这是0)!

C++ 使用 Bind 创建线程的问题

c - 在C中使用Windows API进行多线程

c++ - 有没有办法使用 boost 线程库将线程亲和性设置为处理器内核?