I am following a tutorial and it basically explains about the cause of race condition which happens when resizing Hashmap in a multithreaded environment:
In Java, if two thread at the same time found that now HashMap needs resizing and they both try to resizing. on the process of resizing of HashMap in Java , the element in bucket which is stored in linked list get reversed in order during their migration to new bucket because java HashMap doesn't append the new element at tail instead it append new element at head to avoid tail traversing. If race condition happens then you will end up with an infinite loop
I have two questions after reading this:
Please help me to clarify this, much appreciated!
The answer for your first question is in the quoted text:
"because java HashMap doesn't append the new element at tail instead it append new element at head to avoid tail traversing"
If HashMap would store them in insertion order it had to traverse the list at each insertion or store an extra pointer to the end of the list (and maintain it). Anyhow storing the elements in the bucket in insertion order would not give any benefits (at least I can't think of any).
The answer to your second question relies here:
http://mailinator.blogspot.hu/2009/06/beautiful-race-condition.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With