<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Linuef</title>
    <description>The latest articles on DEV Community by Linuef (@linuef).</description>
    <link>https://dev.to/linuef</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4022817%2F537b5ee9-b5f2-4555-9d0a-e9501abe21a8.png</url>
      <title>DEV Community: Linuef</title>
      <link>https://dev.to/linuef</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/linuef"/>
    <language>en</language>
    <item>
      <title>I’m new to JDK 1.8 HashMap. Please help verify if my understanding is correct.</title>
      <dc:creator>Linuef</dc:creator>
      <pubDate>Thu, 09 Jul 2026 13:03:15 +0000</pubDate>
      <link>https://dev.to/linuef/im-new-to-jdk-18-hashmap-please-help-verify-if-my-understanding-is-correct-2k9j</link>
      <guid>https://dev.to/linuef/im-new-to-jdk-18-hashmap-please-help-verify-if-my-understanding-is-correct-2k9j</guid>
      <description>&lt;h2&gt;
  
  
  1. Core Conclusion
&lt;/h2&gt;

&lt;p&gt;JDK 1.8 HashMap only resolves the &lt;strong&gt;circular linked list infinite loop&lt;/strong&gt; problem in JDK 1.7 during concurrent resizing by adopting the &lt;strong&gt;tail insertion method&lt;/strong&gt;. However, it &lt;strong&gt;fails to fix the data loss issue caused by concurrent resizing&lt;/strong&gt;. HashMap is still not thread-safe, and concurrent resizing by multiple threads will still lead to element loss and data overwriting.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Scenario Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The initial capacity of the HashMap table array is 2&lt;/li&gt;
&lt;li&gt;The linked list at index 1 of the original table: &lt;code&gt;3 -&amp;gt; 7 -&amp;gt; 5&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Two concurrent threads (Thread A and Thread B) execute the &lt;code&gt;resize()&lt;/code&gt; method simultaneously&lt;/li&gt;
&lt;li&gt;JDK 1.8 resizing logic: The original linked list is split into two sub-linked lists based on the high-order bit of the node hash value. The low-order linked list remains at the original index, while the high-order linked list is migrated to the position of &lt;code&gt;original index + original capacity&lt;/code&gt;. Nodes are migrated via the tail insertion method.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Reproduction of Data Loss via Thread Scheduling Sequence
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1: Both threads enter the resizing logic, and Thread A is blocked midway
&lt;/h3&gt;

&lt;p&gt;Both Thread A and Thread B read the old table array simultaneously and start traversing and migrating nodes of the linked list at index 1.&lt;/p&gt;

&lt;p&gt;When Thread A traverses the node &lt;code&gt;e=3&lt;/code&gt; and caches the reference of the next node &lt;code&gt;next=7&lt;/code&gt;, a CPU time slice switch occurs, and Thread A is suspended and blocked.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Thread B completes full resizing and updates the table array
&lt;/h3&gt;

&lt;p&gt;Without being blocked, Thread B completely traverses the entire linked list &lt;code&gt;3-&amp;gt;7-&amp;gt;5&lt;/code&gt; and migrates nodes according to hash rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Element 5: The high-order hash bit is 0, so it remains at index 1 of the new table.&lt;/li&gt;
&lt;li&gt;Elements 3 and 7: The high-order hash bit is 1, so they are migrated to index 3 of the new table, forming the linked list &lt;code&gt;3-&amp;gt;7-&amp;gt;null&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thread B finishes all node migrations and assigns the resized new array to the global&lt;code&gt;table&lt;/code&gt; variable of HashMap, making the old array reference invalid.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Latest table state in memory:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;table[1] = 5&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;table[3] = 3 -&amp;gt; 7 -&amp;gt; null&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 3: Thread A resumes execution and causes data loss via outdated references
&lt;/h3&gt;

&lt;p&gt;After resuming the CPU time slice, Thread A still holds the &lt;strong&gt;outdated reference of the original linked list before resizing&lt;/strong&gt; and continues the traversal loop:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Processes the node &lt;code&gt;e=3&lt;/code&gt; and completes its migration.&lt;/li&gt;
&lt;li&gt;Assigns the cached &lt;code&gt;next=7&lt;/code&gt; to variable &lt;code&gt;e&lt;/code&gt; and continues processing node 7.&lt;/li&gt;
&lt;li&gt;Reads the successor pointer of node 7: &lt;code&gt;7.next = null&lt;/code&gt;, and assigns &lt;code&gt;null&lt;/code&gt; to the &lt;code&gt;next&lt;/code&gt; variable.&lt;/li&gt;
&lt;li&gt;The loop terminates directly as &lt;code&gt;next == null&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Step 4: Final Result – Element 5 is permanently lost
&lt;/h3&gt;

&lt;p&gt;Thread A only traverses and migrates nodes 3 and 7, failing to detect node 5 at all, so no migration operation is performed on element 5.&lt;/p&gt;

&lt;p&gt;After Thread A completes migration and overwrites the table array, element 5 no longer exists in the new array, resulting in &lt;strong&gt;permanent data loss&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>computerscience</category>
      <category>discuss</category>
      <category>java</category>
    </item>
  </channel>
</rss>
