HashMap 的迭代器是快速失败的而 HashTable 的枚举器不是,这到底是什么意思?
我正在查找这两个类之间的区别,这一点出现在很多答案中,此博客是来源: http://javarevisited.blogspot.com/2010/10/difference- Between-hashmap- and.html
但是我不完全明白。 有人可以详细说明一下吗?也许有一个例子?
感谢您的关注!
I was looking up the difference between the two classes and this point came up in a lot of the answers with this blog being the source:
http://javarevisited.blogspot.com/2010/10/difference-between-hashmap-and.html
However I don't completely get it.
Can someone elaborate this? Perhaps with an example?
Thanks for looking in!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
快速失败意味着当您在迭代过程中尝试修改内容时,它将失败并抛出 ConcurrentModificationException。
对于哈希表枚举:
Fail-fast means when you try to modify the content when you are iterating thru it, it will fail and throw ConcurrentModificationException.
For HashTable enumeration:
最好的方法是查看每个类的源代码,如每个类的 Open JDK 实现所实现的那样;这样,你就可以直接从马口中得到答案了:-)
除此之外,本质上,从这个意义上说,“快速失败”意味着如果 HashMap 上的迭代器检测到另一个线程,它就会抛出异常修改了目标 HashMap - 如果您查看 HashMap 的源代码,您将看到这是通过简单地检查预期修改数量的计数器来完成的。如果修改计数与 Iterator 预期的不同,则意味着自上次检查以来有其他人进来并弄乱了 HashMap,因此 Iterator 会抛出 ConcurrentModificationException。
“非快速失败”迭代器不会费心去检查,而是愉快地在底层数据结构中继续它的业务。因此,您获得了一些灵活性(在这种情况下可能是可疑的灵活性),以换取以后可能会遇到错误;即尝试访问不再存在的值。
与所有 fail-fast 策略一样,其想法是越早检测到错误,恢复或调试就越容易。
The best way is to probably look at the source for each class as implemented by the Open JDK implementation for each class; that way, you can get your answer straight from the horse's mouth, as it were :-)
That aside, essentially, "fail-fast" in this sense means that an Iterator over a HashMap will throw an exception if it detects that another thread has modified the targeted HashMap - if you look in the source for HashMap, you will see this is done by simply checking a counter for the number of expected modifications. If the modification count is different than the Iterator expected, that means that someone else has come in since the last check and messed around with the HashMap, and so the Iterator throws a ConcurrentModificationException.
A "non fail-fast" Iterator wouldn't bother to check, and happily go along it's business in the underlying data structure. Therefore, you gain some flexibility (probably dubious flexibility in this case) in exchange for possibly running into errors later; i.e. attempting to access a value that is no longer present.
As with all fail-fast strategies, the idea is that the earlier an error is detected, the easier it is to recover from or debug.
调用 iterator.next() 时,如果在创建迭代器和调用 next() 之间进行了任何修改,则会立即引发 ConcurrentModificationException。这就是快速失败的意思。
Hashtable 返回的枚举没有这种行为。他们假设您知道自己在做什么,并且如果您在使用其枚举之一迭代地图时修改地图,那么据我所知,他们的行为是未定义的。
When calling
iterator.next()
, if any modification has been made between the moment the iterator was created and the momentnext()
is called, a ConcurrentModificationException is immediately thrown. This is what fail-fast means.The Enumerations returned by Hashtable don't have this behavior. They assume you know what you're doing, and their behavior, AFAIK, is undefined if you modify the map while iterating over it using one of its enumerations.