Home » Posts tagged 'what is race condition while doing rehashing of hashmap'

Tag Archives: what is race condition while doing rehashing of hashmap

Advertisements

Rehashing in Java HashMap and Race condition

Hashing – ReHashing and Race condition
Basically while creating a hash map collection assign it a default size (of 10). Later stage when elements are added in the map and after certain stage when you come close to your initial defined capacity there is requirement of ReHashing to retain the performance.

There is LoadFactor defined for the collection (said to be good as .75) and this specifies the good index for time and space.
LARGER load factor => lower space consumption but higher lookups
SMALLER Load factor => Larger space consumption compare to the required no of elements.
Java specification suggests that Good loadfactor value is .75

Hence Suppose you have maximum requirement to store 10 elements in hash then considering the Good Loadfactor .75 = Rehashing would occur after adding 7 elements in the collection. In case if your requirement in this case would not accede to 7 then Rehashing would never occur.

If there are really large no of elements going to be stored in the hashmap then it is always good to create HashMap with sufficient capacity; this is more efficient than letting it to perform automatic rehashing.

RACE condition : While doing the rehasing internal elements which are stored in a linked list for given bucket. They get reverse in the order. Suppose there are two threads encounter the race condition in same time then there are chances of second therad can go in infinite loop while traversal since the order has been changed.

Reference : http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

Advertisements