jdk1.7 HashMap Fatal error: circular list

jdk1.7 HashMap in the "fatal error": circular list

jdk1.7 HashMap configuration in FIG.

jdk1.7 is a linked list structure array +

jdk1.7 versions exist two major problems

  1. The case will result in the first interpolation cycle chain

  2. The list is too long, it will lead to reduced efficiency inquiry

jdk1.8 version optimized for jdk1.8

  1. Tail using interpolation, elimination occurs circular list
  2. After the list is too long, into a red-black tree, improve query efficiency

I specifically refer to another article blog you really know manufacturers face questions: HashMap it?

Produce circular list

Multithreading Meanwhile put, if at the same time calling the resizeoperation could lead to circular list is generated, thus making the time to get back, it will be an endless loop. The following detail how circular list form.

resize function

Array expansion function, the main function is to create a new array after the expansion, and calls the transferfunction will migrate the old elements in the array to a new array

void resize(int newCapacity)
{
    Entry[] oldTable = table;
    int oldCapacity = oldTable.length;
    ......
    //创建一个新的Hash Table
    Entry[] newTable = new Entry[newCapacity];
    //将Old Hash Table上的数据迁移到New Hash Table上
    transfer(newTable);
    table = newTable;
    threshold = (int)(newCapacity * loadFactor);
}

transfer function

transfer logic is actually simple, traversing the old array, the array element by the old header interpolation manner, the position corresponding to migrate to the new problem lies in an array head interpolation .

void transfer(Entry[] newTable)
{
    //src旧数组
    Entry[] src = table;
    int newCapacity = newTable.length;
 
    for (int j = 0; j < src.length; j++) {
        Entry<K,V> e = src[j];
        if (e != null) {
            src[j] = null;
            do {
                Entry<K,V> next = e.next; 
                int i = indexFor(e.hash, newCapacity);
                e.next = newTable[i];
                newTable[i] = e;
                e = next;
            } while (e != null);//由于是链表,所以有个循环过程
        }
    }
}

static int indexFor(int h, int length){
    return h&(length-1);
}

Here's an example of practical

//下面详细解释需要用到这部分代码,所以先标号,将一下代码分为五个步骤
do {
	1、Entry<K,V> next = e.next; 
	2int i = indexFor(e.hash, newCapacity);
	3、e.next = newTab[i];
	4、newTable[i] = e;
	5、e= next;
} while(e != null)
  • Start H a s h M a p HashMap capacity is set to 2, the threshold value is loaded 2 0.75 = 1 2*0.75=1

  • Thread T 2 T_2 And thread T 1 T_1 Meanwhile insert elements, since the threshold value is 1, so the need to call resizethe function, the operation for expansion

  • Thread T 1 T_1 First blocking the code Entry<K,V> next = e.next;, after thread T 2 T_2 After executing expansion of operations

  • After thread T 1 T_1 Wake up, continue, after the completion of a cycle

    Start e 3 e\rightarrow3 , n e x t 7 next\rightarrow7 , implementation of the following code, e 7 e\rightarrow 7 , n e x t 3 next\rightarrow3

    2int i = indexFor(e.hash, newCapacity);
    3、e.next = newTab[i];
    4、newTable[i] = e;
    5、e= next;
    1、Entry<K,V> next = e.next; 
    

  • Thread T 1 T_1 After the implementation of the second cycle

    Start e 7 e\rightarrow 7 , n e x t 3 next \rightarrow 3 , the following code is executed, $ e \ rightarrow3 $, n e x t n in l l next \rightarrow null

    2int i = indexFor(e.hash, newCapacity);
    3、e.next = newTab[i];
    4、newTable[i] = e;
    5、e= next;
    1、Entry<K,V> next = e.next; 
    

  • After the third cycle execution thread T1, forming an endless loop

    Start e 3 e\rightarrow 3 , n e x t n in l l next \rightarrow null , the following code, e n in l l e\rightarrow null

    2int i = indexFor(e.hash, newCapacity);
    3、e.next = newTab[i];
    4、newTable[i] = e;
    5、e= next;
    1、Entry<K,V> next = e.next; 
    

If you execute get (11), 11% 4 = 3, into an infinite loop

references

JDK1.7 HashMap lead to circular list

Published 184 original articles · won praise 220 · Views 140,000 +

Guess you like

Origin blog.csdn.net/zycxnanwang/article/details/105415550