Hash table Data Structure
HashMap usually uses an array of pointers (assuming table[]) to do all the key scattered, when a key is added, the hash algorithm through the key to calculate the array of the subscript I, and then put this <key, value> plug into table[i] , if two different keys are counted in the same I, then it is called conflict, also called collision, this will form a list on table[i].
We know that if the size of table[] is very small, such as only 2, if you want to put in 10 keys, then the collision is very frequent, so an O (1) search algorithm, it becomes the link list traversal, performance becomes O (n), which is the hash table defect.
Therefore, the size and capacity of the hash table is very important. In general, the hash table this container when there is data to be inserted, will check whether the capacity is more than the set of Loadfactor, if more than, need to increase the size of the hash table, but in this way, the whole hash of the entire list of the need to be re-counted. This is called rehash, this cost is quite big.
HashMap's Rehash source code
Put a key,value pair into the hash table:
Publicv put (K key, V value) {...//Calculate hash value inthash =Hash (Key.hashcode ()); inti =indexfor (hash, table.length); //if the key is already inserted, replace the old value (link operation) for(entry<k,v> e = table[i]; E! =NULL; E =e.next) {Object k; if(E.hash = = Hash && (k = e.key) = = Key | |Key.equals (k))) {V OldValue=E.value; E.value=value; E.recordaccess ( This); returnOldValue; }} Modcount++; //The key does not exist and a node needs to be added. addentry (hash, key, value, i); return NULL;}
Check if capacity is exceeded
void addentry (intint bucketindex) { Entry<K,V> e = table[ Bucketindex]; New Entry<k,v>(hash, key, value, e); // Check to see if the current size exceeds the threshold value we set threshold, and if it exceeds, resize if (size++ >= threshold) Resize ( 2 * table.length);}
Create a larger hash table and migrate the data from the old hash table to the new hash table.
void Resize (int newcapacity) { = table; int oldcapacity = oldtable.length; ...... // Create a new hash Table New entry[newcapacity]; // migrating the data on the old hash table to the new hash table Transfer (newtable); = newtable; = (int) (newcapacity * loadfactor);}
Source code of the migration, note the highlight:
voidtransfer (entry[] newtable) {entry[] src=table; intNewcapacity =newtable.length; //The following code means://pick an element from the oldtable and put it in the newtable . for(intj = 0; J < Src.length; J + +) {Entry<K,V> e =Src[j]; if(E! =NULL) {Src[j]=NULL; Do{ Entry <K,V> next = E.next; inti =indexfor (E.hash, newcapacity); E.next = Newtable[i]; Newtable[i] = e; e = next; } while(E! =NULL); } }}
Well, this code is fairly normal. And there's no problem.
The process of normal rehash
I drew a picture and made a presentation.
- I assume that our hash algorithm simply uses the key mod to size the table (that is, the length of the array).
- The top is the old hash table, where the hash table is size=2, so key = 3, 7, 5, after mod 2 all conflict in table[1] here.
- The next three steps are the hash table resize into 4, and then all <key,value> re-rehash the process
Rehash under the concurrency
1) Suppose we have two threads. I marked it in red and light blue.
Let's look back at this detail in our transfer code:
Do { Entry/// <--assumes that a thread is scheduled to be dispatched to suspend the int i = indexfor (E.hash, newcapacity); = Newtable[i]; = E; =whilenull);
And our thread two execution is done. So we have the following look.
Note that because Thread1 's e points to key (3), and next points to key (7), after thread two rehash, it points to the linked list of threads two reorganization . We can see that the order of the linked list is reversed.
2) The line Cheng is dispatched back to execute.
- First executes newtalbe[i] = e;
- Then E = Next, which led to the E pointing to Key (7),
- The next loop, next = E.next, causes next to point to key (3).
3) All is well.
Line Cheng went on to work. Take the key (7) down, put it on the first one of Newtable[i], and move E and next down .
4) Ring link appears.
E.next = Newtable[i] Causes key (3). Next points to key (7)
Note: At this point the key (7). Next has pointed to key (3), and the ring list appears.
So, when our thread was called to Hashtable.get (11), the tragedy appeared--infinite Loop.
La La la
The dead loop of JAVA HashMap