Question

In Java 8 for the Really Impatient Horstmann writes:

If multiple threads modify a plain HashMap, they can destroy the internal structure. Some of the links may go missing, or even go in circles, rendering the data structure unusable. (Section 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?

Was it helpful?

Solution

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.

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top