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

Source code analysis of Hashtable and ConcurrentHashMap

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

Why do you compare and analyze these two data structures? I believe we all understand. First of all, both of them are thread-safe, but the ways in which they ensure thread safety are different. No more nonsense, analyze the similarities and differences between the two from the point of view of the source code, and first give the inheritance diagram of the two.

Source code analysis of Hashtable class attributes and methods

Let's start with a property and method diagram of the Hashtable class, where Entry is the static inner class of the Hashtable class, which inherits from the Map.Entry interface. The meaning of properties and methods in the Hashtable class is explained in detail below.

Attribute

Entry [] table: an array of type Entry, used to store key-value pairs in Hashtable

Int count: stores how many key-value pairs are in the hashtable

Int threshold: when the count value is greater than this value, the hash table expands its capacity and rehash ()

Float loadFactor: the initial size of the threshold= hash table is * loadFactor. The initial capacity defaults to 11 loadFactor. The default value is 0.75.

Int modCount: implement the "fail-fast" mechanism. When iterating over Hashtable in a concurrent collection, if other threads make structural changes to Hashtable, the iterator will compare whether expectedModCount and modCount are consistent, and throw a ConcurrentModificationException exception if they are inconsistent. The following is illustrated by an example of throwing a ConcurrentModificationException exception.

ConcurrentModificationException exception

The remove (Object key) method of Hashtable is shown in method 5 below. Each time the data in hashtable is modified, the value of modCount is updated. The relevant code of the inner class Enumerator of Hashtable is as follows:

Enumerator class

Method

Contains (Object value), the method determines whether the hashtable contains a key-value pair with a value of value, and synchronized is required to execute the method. Empty value is not allowed to be stored in hashtable, so when the lookup value is empty, a null pointer exception is thrown directly. Next, there are two for loops that iterate through table. As you can see from the attributes in the Entry entity class above, the next attribute points to the next entity that has the same hashcode as that entity.

ContainsKey (Object key), the method determines whether the hashtable contains a key-value pair whose key is key, and the execution of the method also requires a lock (synchronized) on the whole table. First, the hashcode is calculated according to the currently given key value, and the hashcode value calculates the subscript in the table array where the key is located, and each Entry object e in the subscript is traversed in turn. Since different hashcode maps to the same subscript in the array, it is first determined whether the hashcode value of e and the hashcode value of the queried key are the same, and if the same is the same, it determines whether the key is equal.

Get (Object key), which gets the value corresponding to the current key key. This method and the containsKey (Object key) method are the same except for the return value. If the value corresponding to the key can be found, the value of value is returned, and if not, null is returned.

Put (K key, V value), adding the key-value pair to the table. The first inserted value cannot be empty. Second, if the currently inserted key value already exists in the table, replace the original value with the new value and return the original value as the return value of the method. If the currently inserted key is not in the table, the key-value pair is inserted.

The insertion method first determines whether the value in the current table is greater than the threshold (threshold). If it is greater than the threshold, first expand the capacity of the table, and then insert the new key-value pair into the position of the first Entry in the linked list of table.

Remove (Object key) to remove the Entry with the key key from the table table. Similarly, this method also needs to lock the entire table table. If the key exists in the table, the value of the deleted key is returned, and if the key does not exist in the current table, the return value of the method is null.

Replace (K key, V value), updates the Entry object value with the key key to value, and takes the original value as the return value of this method.

Source code analysis of ConcurrentHashMap class attributes and methods

ConcurrentHashMap has changed a lot in JDK1.8. It abandons the concept of Segment (segment lock) and uses CAS algorithm in its implementation. The bottom layer uses array + linked list + red-black tree, but in order to achieve concurrency, a large number of auxiliary classes have been added at the same time. The following is the class diagram of ConcurrentHashMap.

Attribute

/ / maximum capacity of ConcurrentHashMap private static final int MAXIMUM_CAPACITY = 1

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

Network Security

Wechat

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

12
Report