In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains why HashMap thread is not safe in Java. Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn why HashMap threads are not safe in Java.
01. Expanding capacity under multithreading will lead to an endless cycle.
As we all know, HashMap solves hash conflicts by zipper method, that is, when hash conflicts occur, key-value pairs with the same hash value are stored in the form of a linked list.
In JDK 7, the linked list is stored by header insertion, that is, the next conflicting key-value pair is placed in front of the previous key-value pair (new elements in the same position are placed in the head of the linked list). Expansion may lead to the emergence of circular linked lists, resulting in an endless loop.
The source code of the resize method:
/ / newCapacity is the new capacity void resize (int newCapacity) {/ / decimal array, temporary excess Entry [] oldTable = table; / / capacity before expansion int oldCapacity = oldTable.length; / / MAXIMUM_CAPACITY is the maximum capacity, 2 to the 30th power = 1 threshold) resize (); afterNodeInsertion (evict); return null;}
The problem occurs here in step ②:
If ((p = tab [I = (n-1) & hash]) = = null) tab [I] = newNode (hash, key, value, null)
Both threads execute the if statement, assuming thread A first executes tab [I] = newNode (hash, key, value, null), then the table looks like this:
Then, thread B executes tab [I] = newNode (hash, key, value, null), and the table goes like this:
3 was killed.
Concurrency of 03, put and get will result in get to null
When thread An executes put, it expands because the number of elements exceeds the threshold, and thread B executes get at this time, which may cause this problem.
Note that the resize source code:
Final Node [] resize () {Node [] oldTab = table; int oldCap = (oldTab = = null)? 0: oldTab.length; int oldThr = threshold; int newCap, newThr = 0; if (oldCap > 0) {/ / if it exceeds the maximum, it will no longer be expanded, so you have to go if (oldCap > = MAXIMUM_CAPACITY) {threshold = Integer.MAX_VALUE; return oldTab. } / / if the maximum value is not exceeded, it will be expanded to twice the original else if ((newCap = oldCap = DEFAULT_INITIAL_CAPACITY) newThr = oldThr 0) / / initial capacity was placed in threshold newCap = oldThr; else {/ / zero initial threshold signifies using defaults newCap = DEFAULT_INITIAL_CAPACITY; newThr = (int) (DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY) } / / calculate the new resize upper limit if (newThr = = 0) {float ft = (float) newCap * loadFactor; newThr = (newCap < MAXIMUM_CAPACITY & & ft < (float) MAXIMUM_CAPACITY? (int) ft: Integer.MAX_VALUE);} threshold = newThr; @ SuppressWarnings ({"rawtypes", "unchecked"}) Node [] newTab = (Node []) new Node [newCap]; table = newTab;}
After thread An executes table = newTab, the table in thread B also changes. When you go to get, of course, you will get to null, because the element has not yet been transferred.
At this point, I believe you have a deeper understanding of "Why HashMap thread is not safe in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.