The principle of "JAVA" HashMap and the cause of the dead loop under multi-thread

Source: Internet
Author: User

again turned to the previous work encountered a problem, hashmap in multi-threaded under the death cycle of the problem, previously just know will die cycle, causing cpu100% to drag the machine across, today to thoroughly see

First of all, the principle of HashMap: HashMap is an array, the key using the hash algorithm to calculate the corresponding subscript I, and then <key, value> plug to Table[i], if two different keys are counted in the same I, then there is a conflict, Also known as collisions, which will form a linked list on table[i]; HashMap is a data structure composed of an array + linked list;

We know that when we put the element into HashMap, there will be a key, we will first take the key subscript, is actually the key corresponding to the type of Hashcode () method, and Hashcode is what?

Referring to Hashcode, first we need to understand a data structure, a hash table (HashTable, also known as a hash table), is directly accessed according to the key code value (key value), which means that it accesses the record by mapping the key code value to a location in the table. To speed up the search. This mapping function is called a hash function, and the array that holds the record is called the hash table.

The hash function makes access to a data series more efficient, and the data elements are positioned more quickly through the hash function.

The Hashcode method in Java is to map the object-related information (such as the object's storage address, the object's field, and so on) into a numeric value, which is called a hash, according to certain rules.

Back to see, hashcode corresponds to the key code value of keys, and then we put the Java code in the string hashcode calculation code to see:

The hashcode is actually calculated based on the value of the character, assuming we want to calculate the hashcode of "ABC", What is the calculation?

A corresponds to an int value of 97,b=98,c=99; then the result calculated from the above calculation should be: 31 * (31 * (31 * 0 + 97) + 98) + 99

The result is not important, but we can see that the hashcode of the string must be unique, because each character corresponds to a different value, it must be different.

Understand the above process, we look at the multi-threaded deadlock under the problem, there are many articles written is very complex, summed up needs to meet the following conditions:

  1. Two threads are processing the same hashmap
  2. Put data when there is a conflict, that is, in table[i] need to store multiple elements (that is, a linked list), too many conflicts, the execution efficiency will fall, so the size of the array is very important
  3. Now assuming that the conflict is more severe, this time the HashMap need to be reorganized, that is, capacity increases, to reduce the conflict
  4. This time need to re-copy the old HashMap elements (actually not copy, but just re-reference address, not new object) to the new HashMap, in Table[i] on the storage of multiple elements, two threads at the same time, resulting in a linked list of the loop list is: A->b && B->a
  5. Assuming that the existing linked list data is A->b->null
      1. The first thread executes when e=a,next=b, pauses
      2. The second thread executes the new linked list and the data is b->a->null;
      3. At this point the first thread continues to execute
        1. The first cycle is a->null,
        2. The second cycle is b->a->null,
        3. Since the second thread has modified the structure of the linked list (b->a->null), when taking the next of B, it takes a
        4. Put A into the new array, the structure is:a->b->a; the old linked list takes the E = null, ending the loop
        5. At this time the linked list has formed A<->b
  6. After the loop linked list appears, the trigger condition is an infinite loop when the data in the Get loop linked list occurs, causing the CPU to be exhausted and the machine hanging out

It is important to understand that when multiple threads are executing, each thread will define a Table object, assuming that two threads are executing, then there are three table

    1. The old table
    2. Table for thread 1
    3. Table for thread 2

But the list of lists is a reference type, that is, the address is the same, when the list changes, it will affect the global map;

There are several points in this: Each table is a reference type, and each of the list structure elements is a reference type, changing the structure of the reference type is a global change, not just a thread variable;

Workaround:

    1. When using HashMap, do not use in multi-threaded environment, if need to use, can use Concurrenthashmap
    2. Use the Synchronized keyword when you really want to use it

PostScript: In the previous discussion, we saw that if the old elements are copied to the new table in the HashMap, there will be no dead loop, but there will be new problems, the elements are never consistent, because the values on each thread are inconsistent, so be sure to pay attention to thread safety when using the collection type in multi-threading

Reference article:

On the Hashcode method in Java:http://www.cnblogs.com/dolphin0520/p/3681042.html

Vaccines: The dead loop of JAVA HashMap:https://coolshell.cn/articles/9606.html

This example is clearer, I read this to understand:51849692

The principle of "JAVA" HashMap and the cause of the dead loop under multi-thread

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.