Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use HashMap and ConcurrentHashMap in java to achieve high concurrency

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to use HashMap and ConcurrentHashMap to achieve high concurrency in java. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

HashMap

At the bottom of the HashMap is an array, and each item of the array is a linked list, and when we create a new HashMap, we initialize an array. HashMap has two parameters that affect performance, which are the initial capacity and the loading factor.

HashMap addressing method: for a newly inserted data or data that needs to be read, HashMap needs to calculate the hash value according to a calculation rule according to key and take the module result as the index of the array, and the cost of taking the module is much higher than the cost of bit operation, so the array length of HashMap must be the N power of 2, and the calculated hash value and the NLY power of 2 must be "and". The result is the same as the modeling operation.

HashMap does not require users to pass in an integer to the Nth power of 2 when specifying the HashMap capacity, but to calculate a capacity that satisfies the Nth power of 2 according to the value of the capacity passed in during initialization.

It is well known that HashMap is not thread-safe, mainly reflected in the fact that an endless loop may occur when executing the resize method. When the size of HashMap exceeds its capacity * load factor, it is necessary to expand HashMap by creating a new array that is twice the original capacity (to ensure that the new capacity is still to the N power of 2, so as to ensure that the above addressing method is still applicable) At the same time, reinserting the original array into the new array does not guarantee thread safety and may result in a dead loop when multithreaded concurrent calls are made.

Above are the steps to expand the capacity of reHash under single thread.

In the case of multithreading:

ConcurrentHashMap

Data structure:

After reading the above, do you have any further understanding of how to use HashMap and ConcurrentHashMap in java to achieve high concurrency? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report