简体   繁体   English

多线程如何破坏Hashmap的内部结构

[英]How can multiple threads corrupt internal structure of Hashmap

In Java 8 for the Really Impatient Horstmann writes: Java 8 for the Really Impatient Horstmann写道:

If multiple threads modify a plain HashMap, they can destroy the internal structure. 如果多个线程修改普通的HashMap,它们可以破坏内部结构。 Some of the links may go missing, or even go in circles, rendering the data structure unusable. 某些链接可能会丢失,甚至会以圆圈形式出现,从而导致数据结构无法使用。 ( Section 6.2.1 ) 6.2.1节

I can understand that unsynchronized concurrent access can corrupt my data. 我可以理解,未同步的并发访问可能会破坏我的数据。 If both two threads update same value one can overwrite the other. 如果两个线程都更新相同的值,则可以覆盖另一个。

But why and how would it corrupt the internal memory structure? 但是为什么以及如何破坏内部存储器结构呢?

The answer you are seeking is perfectly explained in this blog post. 您正在寻找的答案在博文中得到了很好的解释。

If you read through it you will see that a race condition can among other things corrupt the pointers between items in a bucket, thus causing a lookup to turn into an infinite traversal between two items. 如果您仔细阅读它,您将看到竞争条件可能会破坏存储桶中项目之间的指针,从而导致查找变为两个项目之间的无限遍历。

I am not exactly sure how much the implementation of HashMap has changed for Java 8, but I suspect that the basics still apply. 我不确定Java 8的HashMap实现有多少变化,但我怀疑基础知识仍然适用。

I should also add that this problem is not too difficult to encounter, I have actually seen it happen in a real life production system! 我还要补充一点,这个问题不难发现,我实际上已经看到它发生在现实生活系统中!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM