HashMap multi-thread endless loop

Source: Internet
Author: User

HashMap multi-thread endless loop

As mentioned in the previous article, HashMap is NOT thread-safe. It has encountered problems when it is shared by multiple threads. What are the specific problems? It has never been clearly understood, today I wrote a test program and called it to understand the truth.

When multiple threads are put at the same time, if the rehash operation is triggered at the same time, a circular node will appear in the chain table of HashMap, which will lead to an endless loop in the later get. [About what is rehash, You can google it on your own]

This article mainly references: http://coolshell.cn/articles/9606.html, and the test data is similar.

Test code:

import java.util.HashMap;public class HashMapInfiniteLoop {private static HashMap
 
   map = new HashMap
  
   (2,0.75f);public static void main(String[] args) {map.put(5, 55);new Thread("Thread1") {public void run() {map.put(7, 77);System.out.println(map);};}.start();new Thread("Thread2") {public void run() {map.put(3, 33);System.out.println(map);};}.start();}}
  
 
Here, map is initialized to an array with a length of 2, loadFactor = 0.75, threshold = 2*0.75 = 1, that is, when the second key is put, map needs to rehash:

Of

Next, start debugging and running the test program. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + o6gxo6nK18/fingerprint = "http://www.2cto.com/uploadfile/Collfiles/20141106/20141106081535275.png" alt = "\">

In this case, the map content is:


(2) Thread1 debug the JDK source code and stop it in the HashMap. transfer () method.First lineSwitch to Thread2, also in the HashMap. transfer () methodFirst line:


In this case, the map content is:

(3) switch to Thread1 and stop the 477 rows in the HashMap. transfer () method [src [j] = null;]. Switch to Thread2 and stop the 477 rows.


(4) switch back to Thread1 and continue execution. Stop at row 480 in the figure: [int I = indexFor (e. hash, newCapacity );]


At this time, the map content remains unchanged, but the e and next pointers in the Code are added:



(5) switch back to Thread2 and directly complete Thread2 execution:


In this case, the map content is:


(6) switch back to Thread1 and return to the transfer () method:


In this case, we can also clearly see the map content from the debugging information: {5 = 55, 7 = 77, 3 = 33}

At this time, the map is like:


(7) Continue the do-while loop in Thread1. After the first loop, [stop at Row 3 ]:


(8) after the second cycle, continue to [stop at Row 3 ]:


(8) after the second cycle, e is already null and jumps out of the loop:


(9) Exit the transfer () method, return to the resize method, and assign newTable to map. table. In this case, check the map. The default toString () method is already in an endless loop.



At this point, the chain table with the array index position 3 in the map has successfully appeared in the ring, and then the get operation with the index position 3 on the map will end up here, the CPU reaches 100%.

For example, calling map. get (11) will lead to an endless loop.


In addition, elements are lost in map, and (5, 55) are no longer in map.







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.