Infinite loop problem resolved under Concurrent HashMap

First, the junior partner must be clear: infinite loop problem exists before JDK 1.8, JDK 1.8 has been fixed by adding loHead and loTail.

In JDK 1.7 and earlier HashMap cause circulation problems in concurrency, resulting in server cpu soared to 100%, then to resolve today about the HashMap thread-safe in case of high concurrency is how to cause an infinite loop.

Hashmap reason to explore the cycle of death we must first know hashmap hashmap source code in order to be understood fundamentally.

Hashmap first insertion element when the element reaches a threshold number:

First, the junior partner must be clear: infinite loop problem exists before JDK 1.8, JDK 1.8 has been fixed by adding loHead and loTail.

In JDK 1.7 and earlier HashMap cause circulation problems in concurrency, resulting in server cpu soared to 100%, then to resolve today about the HashMap thread-safe in case of high concurrency is how to cause an infinite loop.

Hashmap reason to explore the cycle of death we must first know hashmap hashmap source code in order to be understood fundamentally.

Hashmap first insertion element when the element reaches a threshold number:

addEntry have not reached the threshold for judging barrel, reaching the threshold will go resize method:

resize method in the call transfer method for transferring elements:

The following method is a method cycle of death appeared, Please listen to me 11 years:

Adding elements to achieve this hashmap post-expansion threshold, go reaize method, when the hashmap for expansion, in turn, calls for a transfer of the old hashmap the elements in the transfer, then we have to explore the infinite loop problem today is what has happened in method where, during the transfer of transfer element method calls in the following four lines of code:

    Entry<k,v> next = e.next;
    e.next = newTable[i];
    newTable[i] = e;
    e = next;

The new element into hashmap, the rough look at these four lines of code do not seem to question the transfer of the following chart elements (in the case of threads do not conflict):

Then when multiple threads (A, B threads) simultaneously access our code:

A thread is now time to execute the following code:

    Entry<k,v> next = e.next;

A hand thread time slice, the thread B to take over this time and the transfer completed transfer element, and this time the thread A time to get the sheet and then execute the code:

 

After executing the code as shown, when e = a time, this time to perform this time:

e.next = newTable[i];// a元素指向了b元素 产生循环

So it produces a list circulating at the time get the elements, the thread has been in the ring traversal, unable to jump out, resulting in cpu soar!

Summary: In the case of multiple threads try not to use HashMap, you can use ConcurrentHashMap instead.

A large number of books interview experience and learning materials please pay attention to micro-channel public number: ** AVAJ **

Reply to "offer" be acquired

** 365 ** by plane maker java you want to have me here

Guess you like

Origin www.cnblogs.com/DoubleP/p/11450408.html