HashMap implementation method and device supporting concurrency, medium and equipment

By introducing hash bucket-level operation locks and atomic variable acquisition locks to HashMap, the concurrency problem of HashMap in a multi-threaded environment is solved, achieving high efficiency in multi-threaded operations and data processing.

CN120973797APending Publication Date: 2025-11-18JOINT WARFARE COLLEGE NAT DEFENSE UNIV OF THE CHINESE PEOPLES LIBERATION ARMY
View PDF 5 Cites 0 Cited by

Patent Information

Application Number
CN202510813481.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-06-17
Publication Date
2025-11-18

AI Technical Summary

Technical Problem

HashMap cannot achieve efficient concurrent operations in a multi-threaded environment, resulting in thread blocking and performance degradation.

Method used

By introducing hash bucket-level operation locks to HashMap and using atomic variables to acquire read and write locks, read and write operations are distinguished, achieving a lock-free design and supporting multi-threaded concurrency.

Benefits of technology

It enables efficient concurrent operations of HashMap in a multi-threaded environment, avoiding thread blocking and performance loss, and improving data processing efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120973797A_ABST
    Figure CN120973797A_ABST
Patent Text Reader

Abstract

The invention discloses a HashMap implementation method and device supporting concurrency, a medium and equipment, and the method comprises the steps: for to-be-processed data and a processing mode thereof, obtaining a hash value corresponding to the to-be-processed data according to a hash function currently adopted by the HashMap; determining a hash bucket to which the to-be-processed data belongs according to the hash value; obtaining an operation lock for the hash bucket corresponding to the processing mode by using the atomic variable; and under the condition that the operation lock is successfully obtained, executing corresponding data processing operation on the data chain table in the Hash bucket according to the to-be-processed data and the processing mode thereof, and releasing the operation lock for the Hash bucket after the data processing operation is successfully executed. According to the technical scheme provided by the invention, the HashMap can support multi-thread concurrent operation and has relatively high data processing efficiency, and the HashMap with safe threads is realized, so that the HashMap can be relatively well applicable to application scenes with high-performance and high-concurrency requirements.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present disclosure relates to the field of computer, and particularly relates to a concurrent HashMap implementation method and device, a storage medium and an electronic device. BACKGROUND

[0002] The HashMap can be considered as a collection for storing Key-Value pairs. The HashMap itself is not thread-safe, that is, the HashMap itself does not support multi-thread concurrent operation, thus, the HashMap is usually suitable for single-thread environment and has excellent performance in the single-thread environment, for example, fast lookup, insertion and deletion operations can be implemented.

[0003] How to make the HashMap have good performance while supporting multi-thread concurrent operation is a technical problem worth paying attention to. SUMMARY

[0004] In order to solve the above technical problem, the present disclosure is proposed. Embodiments of the present disclosure provide a concurrent HashMap implementation method and device, a storage medium and an electronic device.

[0005] According to a first aspect of the embodiments of the present disclosure, a concurrent HashMap implementation method is provided, the method comprising: acquiring a hash value corresponding to a to-be-processed data according to a hash function currently used by a HashMap, for the to-be-processed data and a processing mode thereof; wherein the to-be-processed data comprises a to-be-processed key or a to-be-processed key-value pair; the processing mode comprises a read operation, a write operation or a deletion operation; determining a hash bucket to which the to-be-processed data belongs according to the hash value; acquiring an operation lock corresponding to the hash bucket for the processing mode by using an atomic variable; wherein the operation lock corresponding to the hash bucket comprises a read lock for the hash bucket or a write lock for the hash bucket; in a case of successfully obtaining the operation lock, performing a corresponding data processing operation on a data linked list in the hash bucket according to the to-be-processed data and the processing mode thereof; and in a case of successfully performing the data processing operation, releasing the acquired operation lock corresponding to the hash bucket.

[0006] According to a second aspect of the embodiments of the present disclosure, there is provided a device for supporting concurrent HashMap implementation, comprising: a hash value obtaining module configured to obtain a hash value corresponding to a to-be-processed data according to a current hash function, for the to-be-processed data and a processing manner of the to-be-processed data; wherein the to-be-processed data comprises a to-be-processed key or a to-be-processed key-value pair; the processing manner comprises a read operation, a write operation or a delete operation; a hash bucket determining module configured to determine a hash bucket to which the to-be-processed data belongs according to the hash value obtained by the hash value obtaining module; an operation lock obtaining module configured to obtain an operation lock corresponding to the hash bucket for the processing manner by using an atomic variable; wherein the operation lock corresponding to the hash bucket comprises a read lock for the hash bucket or a write lock for the hash bucket; a data processing operation executing module configured to execute a corresponding data processing operation on a data linked list in the hash bucket determined by the hash bucket determining module according to the to-be-processed data and the processing manner, in a case where the operation lock obtaining module successfully obtains the operation lock; and a release operation lock module configured to release the obtained operation lock corresponding to the hash bucket, in a case where the data processing operation executing module successfully executes the data processing operation.

[0007] According to a third aspect of the embodiments of the present disclosure, there is provided a computer readable storage medium, the storage medium storing a computer program, the computer program being configured to implement the above method.

[0008] According to a fourth aspect of the embodiments of the present disclosure, there is provided an electronic device, comprising: a processor; a memory configured to store executable instructions of the processor; and the processor configured to read the executable instructions from the memory and execute the instructions to implement the above method.

[0009] The support-concurrent HashMap implementation method, the implementation device, the medium and the equipment provided by the above-mentioned embodiments of the present disclosure can limit the lock granularity of the operation lock to the hash bucket level by determining the hash bucket to which the data to be processed belongs and obtaining the operation lock for the hash bucket, so that different threads can concurrently perform data processing operations on different hash buckets, which is beneficial to avoid thread blocking phenomenon caused by waiting of other threads when one thread performs data processing operation on the HashMap. The operation lock for the hash bucket can be obtained by using the atomic variable mode, which can realize locking based on the lock-free form and is beneficial to avoid performance loss caused by context switching when using traditional locks. The operation lock for the hash bucket is divided into a read lock for the hash bucket and a write lock for the hash bucket, so that the read lock and the write lock can use different types of locks, such as a shared type of lock for the read lock and an exclusive type of lock for the write lock, which is beneficial to enable multiple threads to perform data processing operation based on the shared type of lock (such as the read lock) on the same hash bucket in the HashMap in parallel, which is beneficial to realize concurrent operation of a large number of threads, thereby improving the data processing efficiency of the HashMap. Therefore, the technical solution provided by the present disclosure can enable the HashMap to support multi-thread concurrent operation, especially high-concurrent operation of a large number of threads, thereby not only realizing thread-safe HashMap, but also enabling the HashMap to have high data query, deletion, modification and writing data processing efficiency, i.e. high-concurrent efficiency, which is further beneficial to enable the HashMap to be better applied to application scenarios with high performance and high concurrency requirements.

[0010] The technical solution of the present disclosure will be described in further detail below by means of the accompanying drawings and embodiments. BRIEF DESCRIPTION OF DRAWINGS

[0011] The above and other objects, features and advantages of the present disclosure will become more apparent from the following detailed description taken in conjunction with the accompanying drawings. The drawings provided in the present disclosure serve to provide a further understanding that assists in elucidating the present disclosure, and constitute a part of the specification. The drawings provided in the present disclosure together with the present disclosure serve to explain the present disclosure, and do not constitute a limitation on the present disclosure. In the drawings, the same reference numerals generally indicate the same components or steps.

[0012] Figure 1 Application scenario schematic diagram of the support-concurrent HashMap implementation technical solution of the present disclosure;

[0013] Figure 2 Flowchart of the support-concurrent HashMap implementation method embodiment of the present disclosure;

[0014] Figure 3A flow chart of an embodiment of the present disclosure for supporting concurrent HashMap implementation of read operation;

[0015] Figure 4 A flow chart of an embodiment of the present disclosure for supporting concurrent HashMap implementation of write operation;

[0016] Figure 5 A flow chart of an embodiment of the present disclosure for supporting concurrent HashMap implementation of rehashing;

[0017] Figure 6 A structural schematic diagram of an embodiment of the present disclosure for supporting concurrent HashMap implementation device;

[0018] Figure 7 A structural diagram of an electronic device provided by an exemplary embodiment of the present disclosure. DETAILED DESCRIPTION

[0019] The exemplary embodiments of the present disclosure will be described in detail with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of the present disclosure, and are not all the embodiments of the present disclosure. It should be understood that the present disclosure is not limited to the exemplary embodiments described herein.

[0020] Summary of the disclosure

[0021] In the process of implementing the present disclosure, the inventors found that, due to the characteristics of multi-threading, such as high hardware resource utilization and low cost of creating threads, and the gradual increase of tools supporting multi-threading, multi-threading has been widely used. Although HashMap has the characteristics of fast access, efficient storage, and strong flexibility, HashMap itself cannot be applied in a multi-threaded environment because it is not thread-safe. If HashMap is applied in a multi-threaded environment, an additional control mechanism needs to be added.

[0022] Currently, a traditional lock control mechanism is usually used to implement a concurrent HashMap, for example, when a thread successfully competes for a lock to perform a read operation (i.e. a query operation) or a write operation on the HashMap, other threads need to wait until the thread currently performing the read operation or the write operation on the HashMap releases the lock. In an application scenario where the read and write operations on the HashMap are frequent (i.e. a high-concurrency application scenario), this method will cause a large number of read operations and write operations to be blocked, and in addition, the traditional lock will also have the problem of performance loss of context switching.

[0023] Research on different data processing operations such as read, write, and delete operations in HashMap reveals that the scope of each operation is usually predictable in advance. For example, it can be known in advance that the scope of a data processing operation is limited to a specific hash bucket in the HashMap and will not affect other hash buckets. Therefore, when adding an additional lock control mechanism to HashMap, locking at the HashMap level is too coarse and the granularity of locking can be further optimized. Furthermore, although the scope of read, write, and delete operations is limited to a single hash bucket, the impact of different data processing operations (such as read and write operations) on the key-value pairs in the hash bucket is significantly different. Therefore, the lock control strength for the hash bucket can vary depending on the data processing operation.

[0024] Example overview

[0025] This disclosed concurrent HashMap implementation solution is applicable to various application scenarios, including data caching, data processing of database query results, data processing of search engine search results, related data storage, and element frequency statistics. The following section combines... Figure 1 An example is given to illustrate an application scenario suitable for counting the frequency of element occurrences.

[0026] Assuming there are multiple elements ( Figure 1 The table only shows elements 1, 2, 3, 4, 5, and 6. We need to count the number of times each element appears within a time period to obtain the frequency of each element within a unit of time.

[0027] First, allocate a HashMap with a predetermined array size (e.g., array size 16) to the application. Figure 1 Only a portion of the HashMap structure is shown, and a hash function is set for this HashMap. The number of hash buckets in this HashMap is related to a predetermined array size (e.g., 16 hash buckets). When a thread performs data processing operations on a corresponding hash bucket, it needs to acquire the operation lock (such as a write lock or read lock) corresponding to that hash bucket. Each thread acquires the write lock and read lock for a hash bucket through CAS (Compare And Swap) atomic variables. Furthermore, each element has a unique identifier to distinguish different elements.

[0028] Secondly, after making the above settings, we began to monitor the appearance of each element.

[0029] Suppose that element 3, element 4 and element 6 are all first-time occurrences, the first thread can perform hash calculation using the identifier of element 3, the second thread can perform hash calculation using the identifier of element 4, and the third thread can perform hash calculation using the identifier of element 6, and each of the three threads obtains a hash value, suppose that the hash value obtained by the first thread corresponds to the first storage location in the HashMap, the hash value obtained by the second thread corresponds to the fourth storage location in the HashMap, and the hash value obtained by the third thread corresponds to the sixth storage location in the HashMap. Each of the three threads obtains a write lock of the hash bucket corresponding to the storage location using a CAS atomic variable, the thread that successfully obtains the write lock of the corresponding hash bucket writes the corresponding key-value pair into the hash bucket, for example, the first thread writes the key-value pair (the identifier of element 3, 1) into hash bucket 1, the second thread writes the key-value pair (the identifier of element 4, 1) into the hash bucket corresponding to the fourth storage location, and the third thread writes the key-value pair (the identifier of element 6, 1) into hash bucket 6. Each of the three threads should release the write lock obtained by the thread after the thread successfully performs the write operation.

[0030] Again, suppose that element 1, element 2 and element 5 are all first-time occurrences, and element 3 is a second-time occurrence, the fourth thread performs hash calculation using the identifier of element 1, the fifth thread performs hash calculation using the identifier of element 2, the sixth thread performs hash calculation using the identifier of element 5, and the seventh thread performs hash calculation using the identifier of element 3, and each of the four threads obtains a hash value. Suppose that the hash values obtained by the fourth thread, the fifth thread and the seventh thread all correspond to the first storage location in the HashMap, and the hash value obtained by the sixth thread corresponds to the sixth storage location in the HashMap. Each of the four threads obtains a write lock of the hash bucket corresponding to the storage location using a CAS atomic variable, the thread that successfully obtains the write lock of the corresponding hash bucket writes the corresponding key-value pair into the hash bucket, since three of the threads obtain write locks of hash bucket 1, when one of the three threads successfully obtains the write lock of hash bucket 1, the other two threads cannot simultaneously have the write lock of hash bucket 1, the two threads can continuously perform the operation of obtaining the write lock of hash bucket 1 until successfully obtaining the write lock of hash bucket 1. For example, suppose that the fifth thread of the fourth thread, the fifth thread and the seventh thread first obtains the write lock of hash bucket 1, the fifth thread writes the key-value pair (the identifier of element 2, 1) into hash bucket 1 using the head insertion method, and releases the write lock of hash bucket 1 obtained by the fifth thread, suppose that the write lock of hash bucket 1 released by the fifth thread is successfully obtained by the fourth thread, the fourth thread writes the key-value pair (the identifier of element 1, 1) into hash bucket 1 using the head insertion method, and forms a structure as shown in FIG. 4. Figure 1The data linked list in the hash bucket 1 shown in the figure, the fourth thread releases the write lock for the hash bucket 1 that it has obtained, assuming that the write lock for the hash bucket 1 that is released is obtained by the seventh thread, the seventh thread modifies the key-value pair (the identity of element 3, 1) in the data linked list in the hash bucket 1 to (the identity of element 3, 2), and releases the write lock for the hash bucket 1 that it has obtained; the sixth thread writes (the identity of element 5, 1) into the hash bucket 6 by using the head insertion method after successfully obtaining the write lock for the hash bucket 6, forming a data linked list as shown in the figure. Figure 1 The data linked list in the hash bucket 6 shown in the figure, the sixth thread releases the write lock for the hash bucket 6 that it has obtained.

[0031] While monitoring the occurrences of the elements, the corresponding operation can also be performed for the requirement of querying the occurrence times of the elements, for example, assuming that the occurrence times of element 5 and element 6 need to be queried, the eighth thread can perform hash calculation by using the identity of element 5, and the ninth thread can perform hash calculation by using the identity of element 6, the two threads each obtain a hash value, and the two hash values both correspond to the sixth storage location in the HashMap. The two threads respectively obtain the read lock of the hash bucket 6 corresponding to the sixth storage location by using the CAS atomic variable, since the read lock is not an exclusive type of lock, therefore, the two threads can simultaneously have the read lock for the hash bucket 6, after successfully obtaining the read lock for the hash bucket 6, the eighth thread queries the node matching the identity of element 5 in the data linked list in the hash bucket 6 by using the identity of element 5, thereby obtaining the occurrence time of element 5 from the value in the key-value pair stored in the matching node, and the ninth thread queries the node matching the identity of element 5 in the data linked list in the hash bucket 6 by using the identity of element 6, thereby obtaining the occurrence time of element 6 from the value in the key-value pair stored in the matching node. The two threads release the read lock for the hash bucket 6 that they have obtained respectively after successfully obtaining the occurrence times of the corresponding elements.

[0032] Example method

[0033] Figure 2 A flowchart of an embodiment of the HashMap implementation method supporting concurrency of the present disclosure. As shown in the figure, the method comprises S200, S201, S202, S203 and S204. Figure 2 The steps in S200, S201, S202, S203 and S204 are described as follows. Figure 2

[0034] S200, for a to-be-processed data and a processing mode thereof, obtaining a hash value corresponding to the to-be-processed data according to a current hash function of the HashMap.

[0035] ​The to-be-processed data of the present disclosure can be a to-be-processed key (such as key) or a to-be-processed key-value pair (such as (key, value)). The processing mode in the present disclosure can be a read operation (such as a get operation), a write operation (such as a put operation), a delete operation (such as a remove operation), or the like. The write operation can be implemented to add a new key-value pair in a hash bucket or to modify the value in a key-value pair in the hash bucket, that is, the write operation can be used to implement a modification operation of a key-value pair.

[0036] The hash function in the present disclosure refers to a function that can obtain a fixed-length output value through transformation of an input of an arbitrary length. For example, the hash function can be MD5 (Message-Digest Algorithm), SHA-1 (Secure Hash Algorithm), SHA256, or SHA512, or the like. The current hash function in the present disclosure refers to a hash function currently used by HashMap. The present disclosure can use the current hash function to perform hash calculation on the key in the to-be-processed key or the to-be-processed key-value pair, and use the hash calculation result as the hash value corresponding to the to-be-processed data.

[0037] S201. Determine the hash bucket to which the to-be-processed data belongs according to the hash value.

[0038] The hash bucket in the present disclosure is a basic storage unit for storing data in HashMap. One hash bucket can store one or more key-value pairs, and the hash bucket can store one or more key-value pairs in the form of a data linked list. Each hash value in the present disclosure corresponds to a hash bucket, and different hash values usually correspond to different hash buckets, that is, there is a one-to-one correspondence between the hash value and the hash bucket. The hash bucket corresponding to a hash value is the hash bucket to which the to-be-processed data corresponding to the hash value belongs.

[0039] In one example, the present disclosure can use the hash value to search for corresponding relationship information matching the hash value in the pre-set corresponding relationship information of the hash value and the hash bucket identifier, so as to determine the hash bucket to which the to-be-processed data belongs according to the hash bucket identifier in the matched corresponding relationship information. The present disclosure can also calculate the hash value and use the calculation result as the hash bucket identifier corresponding to the hash value, so as to determine the hash bucket to which the to-be-processed data belongs. The present disclosure does not limit the specific implementation mode of determining the hash bucket corresponding to a hash value.

[0040] S202. Use the atomic variable to obtain an operation lock for the hash bucket corresponding to the processing mode of the to-be-processed data.

[0041] The atomic variable in the present disclosure is a basic unit of atomic operation, that is, an atomic operation is performed on the atomic variable. The atomic operation herein refers to an operation that cannot be interrupted by a scheduling mechanism (such as a thread scheduling mechanism), that is, once the atomic operation starts to execute, it will run to the end, and there is usually no context switching phenomenon in the middle, thereby facilitating the avoidance of data confusion phenomenon caused by execution context switching when multiple threads are parallel to perform data processing operations on the HashMap.

[0042] In one example, the atomic variable of the present disclosure can be a CAS atomic variable. The CAS atomic variable is an atomic variable implemented based on a CAS algorithm. The present disclosure can achieve a concurrent HashMap based on a lock-free mechanism by using an atomic variable (such as a CAS atomic variable) to obtain an operation lock for a hash bucket.

[0043] The specific form of the operation lock for the hash bucket in the present disclosure is usually determined by the processing method of the data to be processed, that is, the specific form of the operation lock for the hash bucket is related to the processing method of the data to be processed. For example, the specific form of the operation lock for the hash bucket is usually determined according to the exclusive demand of the hash bucket based on the processing method of the data to be processed. In one specific example, when the processing method of the data to be processed is query processing (i.e., read operation), the operation lock for the hash bucket can be specifically a read lock for the hash bucket, and when the processing method of the data to be processed is write processing or deletion processing, the operation lock for the hash bucket can be specifically a write lock for the hash bucket. The read lock for the hash bucket usually does not have exclusivity, for example, multiple threads can concurrently have a read lock for the same hash bucket; for example, multiple threads can concurrently have a read lock for different hash buckets. The write lock for the hash bucket usually has exclusivity, for example, only one thread can have a write lock for the same hash bucket, and other threads cannot concurrently have a write lock for the same hash bucket or a read lock for the same hash bucket; for example, multiple threads can concurrently have a write lock for different hash buckets, and any hash bucket can have a write lock for only one thread, that is, the exclusivity of the write lock for the hash bucket in the present disclosure is limited within one hash bucket and does not affect other hash buckets.

[0044] In one example, for any thread, the process of obtaining the read lock for the hash bucket corresponding to the processing mode of the to-be-processed data by using the CAS atomic variable can be: the thread judges the expected original value in the CAS atomic variable, if the result of the judgment is that the expected original value in the CAS atomic variable is not the first value, the thread changes the expected original value in the CAS atomic variable to the sum of the expected original value and the predetermined step, so that the thread successfully obtains the read lock for the hash bucket corresponding to the processing mode; if the result of the judgment is that the expected original value in the CAS atomic variable is the first value, the thread can return to the step of judging the expected original value in the CAS atomic variable until the thread successfully obtains the read lock for the hash bucket corresponding to the processing mode. The first value indicates that the hash bucket is currently in a write lock state, for example, the first value can be -1, etc. The predetermined step is an integer not less than 1, for example, the predetermined step can be 1, etc. By judging whether the expected original value in the CAS atomic variable is the first value, the read lock of the hash bucket can be conveniently obtained without context switching, realizing the read lock of the lock-free design, which is beneficial to improve the execution efficiency of the read operation and is beneficial to realize the thread-safe Hashmap. By accumulating the expected original value and the predetermined step, the number of threads concurrently executing the read operation for the same hash bucket can be clearly indicated, which is beneficial to real-time understanding of the running situation of the Hashmap and is beneficial to knowing the support degree of the Hashmap for concurrent threads.

[0045] In one example, for any thread, the process of obtaining the write lock for the hash bucket corresponding to the processing mode of the to-be-processed data by using the CAS atomic variable can be: the thread judges the expected original value in the CAS atomic variable, and if the result of the judgment is that the expected original value in the CAS atomic variable is the second value, the thread can change the expected original value in the CAS atomic variable to the first value (such as -1), so that the thread successfully obtains the write lock for the hash bucket corresponding to the processing mode; if the result of the judgment is that the expected original value in the CAS atomic variable is not the second value, the thread can return to the step of judging the expected original value in the CAS atomic variable until the thread successfully obtains the write lock for the hash bucket corresponding to the processing mode. The first value indicates that the hash bucket is currently in a write lock state. The second value (such as 0) indicates that the hash bucket is currently in a lock-free state, that is, the hash bucket is currently neither subjected to a read lock nor subjected to a write lock. By judging whether the expected original value in the CAS atomic variable is the second value and by updating the expected original value to the first value, not only can the write lock of the hash bucket be conveniently obtained without context switching, realizing the write lock of the lock-free design, but also the data conflict phenomenon caused by the data in the hash bucket being read during the write operation can be avoided, so as to improve the execution efficiency of the query operation, the modification operation, the write operation and the deletion operation, and to ensure the accuracy of various data processing operations, which is conducive to realizing the thread-safe Hashmap.

[0046] S203, in the case of successfully obtaining the operation lock, performing a corresponding data processing operation on the data linked list in the hash bucket according to the to-be-processed data and the processing mode thereof.

[0047] The data linked list in the present disclosure can be a data linked list storing one or more key-value pairs, that is, a non-empty data linked list; the data linked list can also be a data linked list not storing key-value pairs, that is, an empty data linked list, for example, the head node of the data linked list is empty. The key-value pair can be stored in the node of the data linked list, and one node can include a data field and a pointer field, wherein the data field is used to store the key-value pair, and the pointer field is used to store the storage location of the next node (i.e. the downstream node of the current node).

[0048] In one example, in the case that the to-be-processed data is a to-be-processed key and the processing mode of the to-be-processed key is a read operation, the present disclosure can find a node matching the to-be-processed key in the data linked list in the hash bucket (such as judging whether the to-be-processed key and the key in the data field of the node are the same in turn according to the arrangement order of the nodes in the data linked list), if the node matching the to-be-processed key is successfully found, the value is obtained from the data field of the matching node, and the value is returned. If the node matching the to-be-processed key is not successfully found, an empty value can be returned.

[0049] In one example, in the case that the data to be processed is a key-value pair (key, value) to be processed, and the processing manner of the key-value pair to be processed is a write operation, the present disclosure can write the key-value pair to be processed in the data linked list in the hash bucket, for example, first find a node matching the key in the key-value pair to be processed in the data linked list in the hash bucket (for example, sequentially determine whether the key to be processed is the same as the key in the data field in the node according to the arrangement order of the nodes in the data linked list), if a node matching the key in the key-value pair to be processed is not successfully found, write the key-value pair to be processed in the data linked list in the hash bucket, thereby adding a new node in the data linked list in the hash bucket, for example, use the head insertion method to write the key-value pair to be processed in the data linked list in the hash bucket; if a node matching the key in the key-value pair to be processed is successfully found, update the value stored in the data field in the matched node with the value in the key-value pair to be processed, thereby updating the key-value pair stored in the existing node with the write operation, completing the modification of the key-value pair, that is, completing the modification operation with the write operation.

[0050] In one example, in the case that the data to be processed is a key to be processed, and the processing manner of the key to be processed is a delete operation, the present disclosure can find a node matching the key to be processed in the data linked list in the hash bucket (for example, sequentially determine whether the key to be processed is the same as the key in the data field in the node according to the arrangement order of the nodes in the data linked list), if a node matching the key to be processed is successfully found, the node can be deleted, and when the node is deleted, the modification of the pointer field of the upstream node should be made; if a node matching the key to be processed is not successfully found, the present disclosure can return null.

[0051] In one example, in the case that the data to be processed is a key-value pair (key, value) to be processed, and the processing manner of the key-value pair to be processed is a delete operation, the present disclosure can find a node matching the key and value in the key-value pair to be processed in the data linked list in the hash bucket (for example, sequentially determine whether the key-value pair to be processed is the same as the key-value pair in the data field in the node according to the arrangement order of the nodes in the data linked list), if a node matching the key-value pair to be processed is successfully found, the matched node can be deleted, and when the matched node is deleted, the modification of the pointer field of the upstream node should be made; if a node matching the key-value pair to be processed is not successfully found, the present disclosure can return null.

[0052] By using the read lock and the write lock of the lock-free design, the query operation, the write operation, the modification operation, and the delete operation on the data linked list in the hash bucket can be quickly implemented, so that the HashMap can support multiple concurrent operations, and the HashMap has good concurrent efficiency.

[0053] S204, release the operation lock for the hash bucket obtained in the case of successful execution of the above data processing operation.

[0054] In one example, the way the present disclosure releases the operation lock for a hash bucket is related to the way the operation lock for the hash bucket is obtained, for example, in the case of obtaining a read lock for a hash bucket by changing the expected original value in the CAS atomic variable corresponding to the hash bucket to the sum of the expected original value and a predetermined step length, the way the present disclosure releases the read lock for the hash bucket can be to change the expected original value in the CAS atomic variable corresponding to the hash bucket to the difference between the expected original value and the predetermined step length; again for example, in the case of obtaining a write lock for a hash bucket by changing the expected original value in the CAS atomic variable corresponding to the hash bucket to a first value (such as -1), the way the present disclosure releases the write lock for the hash bucket can be to change the expected original value in the CAS atomic variable corresponding to the hash bucket to a second value (such as 0).

[0055] In one example, the HashMap of the present disclosure may, in the application process, have the need for expansion, for example, when it is detected that the number of key-value pairs stored in the HashMap meets a first predetermined condition, expansion processing needs to be performed on the current HashMap, that is, a new HashMap is allocated, and all key-value pairs in the current HashMap (i.e., the HashMap before migration, hereinafter referred to as the original HashMap) are migrated to the new HashMap. The first predetermined condition can be that the number of key-value pairs stored in the HashMap reaches or exceeds a first predetermined threshold, or that the load factor calculated using the number of key-value pairs stored in the HashMap reaches or exceeds a second predetermined threshold (such as 0.75), etc. The load factor can be considered as an indicator reflecting the fullness of the HashMap. Generally, the ratio of the number of key-value pairs already stored in the HashMap to the capacity of the HashMap (such as the length of the HashMap) is taken as the load factor.

[0056] In one example, the storage space of the new HashMap is usually twice the storage space of the original HashMap, of course, it can also be more multiples, but the storage space of the new HashMap is usually 2 raised to the power of N (N is an integer greater than 4).

[0057] In one example, in the case that the operation lock of the hash bucket to which the to-be-processed data belongs is successfully obtained for the to-be-processed data and the processing manner thereof, the disclosure can perform a corresponding expansion operation on the hash bucket to which the to-be-processed data belongs, that is, perform an operation of migrating each key-value pair in the data linked list in the hash bucket to which the to-be-processed data belongs to the new HashMap. The hash functions used by the new HashMap and the original HashMap are usually the same. After each key-value pair in the data linked list in all hash buckets in the original HashMap is migrated to the new HashMap, the disclosure can release the original HashMap (that is, delete the original HashMap), so that the new HashMap becomes the current HashMap. Of course, the original HashMap can also be redistributed, and the disclosure does not limit the processing manner of the original HashMap.

[0058] In some scenarios, the present disclosure can, after successfully obtaining an operation lock of a hash bucket to which the to-be-processed data belongs, and before performing a data processing operation on the to-be-processed data, respectively migrate all key-value pairs in a data linked list in the hash bucket to which the to-be-processed data belongs to a new HashMap, and then perform the data processing operation on the to-be-processed data in the new HashMap. For example, for a hash bucket to which a to-be-processed key-value pair belongs, after a process obtains a write lock of the hash bucket, the process or another thread respectively performs new HashMap-based hash calculation on the keys in all nodes in a data linked list in the hash bucket by using a hash function, and determines the storage positions of each of the nodes in the data linked list in the hash bucket in the new HashMap according to the results obtained by the hash calculation. Then, all the nodes in the data linked list in the hash bucket are migrated to the new HashMap according to the currently obtained storage positions. After that, the process that successfully obtains the write lock can obtain a write lock of the hash bucket to which the to-be-processed key-value pair belongs for the new HashMap, and write the to-be-processed key-value pair into the data linked list in the hash bucket to which the to-be-processed key-value pair belongs in the new HashMap (i.e., add a new node in the data linked list in the new HashMap). For another example, for a hash bucket to which a to-be-processed key-value pair belongs, after a process obtains a write lock of the hash bucket, the process or another process respectively performs new HashMap-based hash calculation on the keys in all nodes in a data linked list in the hash bucket by using a hash function, and determines the storage positions of each of the nodes in the data linked list in the hash bucket in the new HashMap according to the results obtained by the hash calculation. Then, all the nodes in the data linked list in the hash bucket are migrated to the new HashMap according to the currently obtained storage positions. After that, the process that successfully obtains the write lock can obtain a write lock of the hash bucket to which the to-be-processed key-value pair belongs for the new HashMap, and modify the value in a matching node in the data linked list in the hash bucket according to the value in the to-be-processed key-value pair (i.e., without adding a new node in the data linked list).

[0059] In some scenarios, the present disclosure can migrate all key-value pairs in the data linked list in the hash bucket to which the to-be-processed data belongs to the new HashMap after successfully obtaining the operation lock of the hash bucket to which the to-be-processed data belongs and after performing the data processing operation on the to-be-processed data, and after successful migration, no longer perform the data processing operation on the to-be-processed data in the new HashMap. For example, for the hash bucket to which the to-be-processed key belongs, after a process obtains the read lock of the hash bucket and successfully obtains the corresponding value from the matching node in the data linked list in the hash bucket, if the hash bucket is currently not performing a migration operation, the thread or another thread uses the hash function to perform new HashMap-based hash calculation on the keys in all nodes in the data linked list in the hash bucket, respectively, and determines the storage location of each node in the data linked list in the hash bucket in the new HashMap according to the result obtained by the hash calculation, and migrates all nodes in the data linked list in the hash bucket to the new HashMap according to the obtained storage locations. For another example, for the hash bucket to which the to-be-processed key-value pair belongs, after a process obtains the write lock of the hash bucket and successfully writes the to-be-processed key-value pair into the data linked list in the hash bucket (i.e., a new node is added to the data linked list), the thread or another thread uses the hash function to perform new HashMap-based hash calculation on the keys in all nodes in the data linked list in the hash bucket, respectively, and determines the storage location of each node in the data linked list in the hash bucket in the new HashMap according to the result obtained by the hash calculation, and migrates all nodes in the data linked list in the hash bucket to the new HashMap according to the obtained storage locations. For another example, for the hash bucket to which the to-be-processed key-value pair belongs, after a process obtains the write lock of the hash bucket and successfully modifies the value in the matching node in the data linked list in the hash bucket according to the value in the to-be-processed key-value pair (i.e., no new node is added to the data linked list), the thread or another thread uses the hash function to perform new HashMap-based hash calculation on the keys in all nodes in the data linked list in the hash bucket, respectively, and determines the storage location of each node in the data linked list in the hash bucket in the new HashMap according to the result obtained by the hash calculation, and the thread can migrate all nodes in the data linked list in the hash bucket to the new HashMap according to the obtained storage locations.

[0060] In some scenarios, the data processing operation performed on the to-be-processed data and the migration operation performed on the hash bucket to which the to-be-processed data belongs can be executed in parallel by two processes. For example, for the hash bucket to which the to-be-processed key belongs, after a process acquires a read lock of the hash bucket, the process searches for a matching node in the data linked list of the hash bucket using the to-be-processed key, while another thread, upon determining that the hash bucket currently does not perform a migration operation, respectively performs new HashMap-based hash calculation on the keys in all nodes in the data linked list in the hash bucket using a hash function, and determines the storage positions of each of the nodes in the data linked list in the hash bucket in the new HashMap according to the results of the hash calculation, and migrates all the nodes in the data linked list in the hash bucket to the new HashMap according to the obtained storage positions.

[0061] By migrating each node in the data linked list in the hash bucket to the new HashMap after acquiring an operation lock of the hash bucket, the gradual expansion is realized, which is conducive to reducing the performance jitter of the HashMap as much as possible during the expansion, thereby being conducive to making the HashMap also have good performance during the expansion, and being conducive to improving the performance stability of the HashMap.

[0062] In an example, the HashMap of the present disclosure can have a demand for adjusting the hash algorithm (i.e., updating the hash algorithm) during application. For example, upon detecting that the performance of the HashMap meets a second predetermined condition, it is determined that the hash algorithm currently used by the HashMap needs to be adjusted. The performance of the HashMap can include at least one of the average time consumption of a query (i.e., a read operation), the longest time consumption of a query, the collision frequency, and the total number of collisions. The second predetermined condition can include that the performance (such as the time consumption) of the HashMap reaches or exceeds a third predetermined threshold, or that the collision (such as the frequency or the total number of collisions) of the HashMap reaches or exceeds a fourth predetermined threshold, and the like. The present disclosure does not limit this.

[0063] In an example, the present disclosure can determine whether the performance of the current HashMap meets the second predetermined condition when a collision occurs, or can determine whether the performance of the current HashMap meets the second predetermined condition when the time length of a certain query exceeds a threshold. The present disclosure does not limit the action of triggering the detection of whether the performance of the HashMap meets the second predetermined condition.

[0064] In one example, the present disclosure is provided with a set of candidate hash functions, which contains at least one candidate hash function, and the present disclosure can select a hash function from part or all of the candidate hash functions in the set, and then acquire write locks for all hash buckets in the HashMap by using an atomic variable (such as a CAS atomic variable), and in the case of successfully acquiring write locks for all hash buckets, perform rehashing on all key-value pairs in the HashMap according to the selected new hash function. Any candidate hash function in the present disclosure and the current hash function can be hash functions based on different algorithms, or hash functions with the same algorithm but different parameters.

[0065] By monitoring the performance of the HashMap and performing rehashing on the HashMap, dynamic adjustment of the hash function can be achieved, i.e., dynamic rehashing, which is beneficial to avoid the phenomenon of serious degradation of time complexity required for query operations, write operations, modification operations, and deletion operations, etc. For example, it can avoid the phenomenon that the time complexity required for the corresponding operations degrades to infinitely close to O(N), thereby being beneficial to ensure that the HashMap always has good performance, such as good data processing efficiency and good concurrency efficiency, etc.

[0066] In one example, the present disclosure can select a hash function from part or all of the candidate hash functions by performance evaluation. Specifically, each candidate hash function in the set of candidate hash functions can be used to perform hash calculation on the keys in each node in the data linked list in part of the hash buckets (such as hash buckets with data linked lists exceeding a certain length) or all hash buckets in the HashMap, thereby obtaining multiple hash values, and the performance of each candidate hash function can be evaluated (such as comparing the number of collisions) by using all hash values corresponding to each candidate hash function, thereby obtaining multiple performance evaluation results, selecting the optimal performance evaluation result, and selecting the candidate hash function corresponding to the optimal performance evaluation result as the new hash function. Of course, the present disclosure does not exclude the case of selecting a hash function from all candidate hash functions by random selection or other ways.

[0067] In one example, the present disclosure can allocate a new HashMap when performing rehashing by using the finally selected candidate hash function, thereby migrating all key-value pairs in the original HashMap to the new HashMap according to the hash values calculated by using the candidate hash function, and the storage space of the new HashMap can be the same as that of the original HashMap.

[0068] By evaluating the performance of each candidate hash function, and using the performance evaluation result to determine a new hash function, it is beneficial to avoid the new hash function causing the HashMap to have performance regression, to help the HashMap after the rehashing process to get rid of the performance degradation, and to ensure that the HashMap has the best possible performance.

[0069] In one example, the pseudo code for implementing the write operation or modification operation using the write lock is as follows:

[0070] / / Algorithm

[0071] Algorithm 0

[0072] / / Calculate the hash value of the key (key is the key to be processed)

[0073] int hashCode = getHashCodeByKey(key);

[0074] / / Calculate the hash bucket to which the key belongs

[0075] Bucket bucket = getBucketByHashCode(hashCode);

[0076] / / If the expected original value in the CAS atomic variable is 0 (indicating that the hash bucket is currently in a lock-free state), change the expected original value to -1 (indicating that the write lock is applied to the hash bucket, i.e., obtaining the write lock of the hash bucket), if the expected original value in the CAS atomic variable is not 0, spin While (!bucketLock.CAS(0, -1))

[0077] / / Give up the time slice and wait for the next write lock competition Thread.sleep(0);

[0078] End While

[0079] / / Write data

[0080] writeData();

[0081] / / Release the write lock bucketLock.setValue(0);

[0082] In one example, the pseudo code for implementing the query operation using the read lock is as follows:

[0083]

[0084]

[0085] In one example, the pseudo code of the dynamic rehashing implementation process in the present disclosure is as follows:

[0086]

[0087]

[0088] In one example, the present disclosure implements the query operation by using the read lock, so that the flow of the method for supporting concurrency of the HashMap is as shown in Figure 3

[0089] Figure 3 In S300, a to-be-processed key and data processing mode information of a value corresponding to the to-be-processed key are obtained, and a read operation (i.e., a query operation) for the to-be-processed key is started.

[0090] In S301, a hash value of the to-be-processed key is calculated by using a hash function of the HashMap.

[0091] In S302, a hash bucket to which the to-be-processed key belongs is obtained by calculating the hash value obtained in the above calculation.

[0092] In S303, it is judged whether the hash bucket to which the to-be-processed key belongs is subjected to an exclusive type of lock (such as judging whether an expected original value of a CAS atomic variable is -1), i.e., a write lock. If the hash bucket is subjected to the exclusive type of lock (such as the expected original value being -1), the process goes to S307. If the hash bucket is not subjected to the exclusive type of lock (such as the expected original value being an integer greater than or equal to 0), the process goes to S304.

[0093] In S304, a read lock of the hash bucket to which the to-be-processed key belongs is obtained, and the number of read locks of the hash bucket to which the to-be-processed key belongs is updated, such as updating the current number of read locks of the hash bucket to the current number+1.

[0094] In S305, a node matching the to-be-processed key is searched for in the hash bucket to which the to-be-processed key belongs, and a value in the key-value pair in the data field in the matching node is read and returned.

[0095] In S306, after successfully reading the value in the key-value pair in the data field in the matching node, the read lock currently obtained is released, such as updating the current number of read locks of the hash bucket to the current number-1.

[0096] In S307, a yield operation is performed, such as giving up the time slice to wait for the next read lock competition, and the process returns to S303.

[0097] In S308, the read operation (i.e., the query operation) for the to-be-processed key ends.

[0098] ​In one example, the present disclosure implements a write lock to implement a write operation, so that the flow of the method for supporting concurrency of HashMap is as shown in Figure 4

[0099] Figure 4 In S400, the data processing method information of a to-be-processed key value pair and the writing of the to-be-processed key into the Hashmap are obtained, and the write operation for the to-be-processed key is started.

[0100] In S401, the hash function of the Hashmap is used to perform hash calculation on the key in the to-be-processed key value pair, to obtain a hash value.

[0101] In S402, the hash value obtained by the above calculation is calculated to obtain the hash bucket to which the to-be-processed key value pair belongs.

[0102] In S403, it is judged whether the hash bucket to which the to-be-processed key value pair belongs is subjected to an exclusive type of lock (such as judging whether the expected original value of the CAS atomic variable is -1), i.e. a write lock. If the hash bucket is subjected to an exclusive type of lock (such as the expected original value being -1), it goes to S408. If the hash bucket is not subjected to an exclusive type of lock (such as the expected original value being an integer greater than or equal to 0), it goes to S404.

[0103] In S404, it is judged whether the number of read locks applied to the hash bucket is 0. If the number is not zero, it goes to S408. If the number is zero, it goes to S405.

[0104] In S405, the write lock of the hash bucket to which the to-be-processed key belongs is obtained, and the lock state of the hash bucket to which the to-be-processed key belongs is updated, such as updating the lock state of the hash bucket to the state of being subjected to an exclusive type of lock.

[0105] In S406, the to-be-processed key value pair is written into the hash bucket to which it belongs.

[0106] In S407, after the to-be-processed key value pair is successfully written into the hash bucket, the currently obtained write lock is released, such as updating the expected original value in the CAS atomic variable of the hash bucket to 0.

[0107] In S408, the yield operation is performed, such as giving up the time slice to wait for the next read lock competition, and returning to S403.

[0108] In S409, the write operation for the to-be-processed key value pair is ended.

[0109] In one example, the present disclosure implements a write lock to implement a write operation, so that the flow of the method for supporting concurrency of HashMap is as shown in Figure 5

[0110] Figure 5 ​​In the method, S500, a key-value pair to be processed and data processing mode information of the key to be written into the Hashmap are obtained, and the writing operation for the key to be processed is started.

[0111] S501, it is judged whether the number of current hash collision (i.e. hash collision) exceeds a preset threshold. For example, after the hash calculation for the key in the key-value pair to be processed, if it is found that the calculation result of this time appears collision phenomenon, it is judged whether the number of current hash collision exceeds a preset threshold. If the number of current hash collision exceeds the preset threshold, S503 is reached. If the number of current hash collision does not exceed the preset threshold, S502 is reached. If the calculation result of this time does not appear collision phenomenon, the judgment step of whether the number of current hash collision exceeds the preset threshold can not be executed, and S502 is directly reached.

[0112] S502, the writing operation for the key-value pair to be processed is executed, and the method of dynamic rehashing of this time is ended.

[0113] S503, an asynchronous task is started to implement the method of dynamic rehashing.

[0114] S504, the process of finding the optimal candidate hash function from the candidate hash function set is started.

[0115] S505, a candidate hash function is obtained from the candidate hash function set.

[0116] S506, the nodes in the data linked list in all hash buckets in the Hashmap are calculated by using the candidate hash function, a plurality of hash values are obtained, and the performance of the candidate hash function is evaluated by using all the hash values.

[0117] S507, it is judged whether there is an unselected candidate hash function in the candidate hash function set. If there is an unselected candidate hash function, S505 is returned. If there is no unselected candidate hash function, S508 is reached.

[0118] S508, the performance evaluation results of each candidate hash function are compared, and the candidate hash function corresponding to the optimal performance evaluation result, i.e. the optimal candidate hash function, is obtained. The optimal candidate hash function is used as the current hash function of the HashMap, and the rehashing processing of the HashMap is performed by using the optimal candidate hash function.

[0119] S509, the rehashing processing of this time is ended.

[0120] Example apparatus

[0121] Figure 6 A structural schematic diagram of a specific embodiment of the HashMap implementation device supporting concurrency of the present disclosure. The device of this embodiment can be used to implement the HashMap implementation device of the present disclosureFigures 2-5 The method embodiment shown.

[0122] like Figure 6 The illustrated apparatus includes: a hash value acquisition module 600, a hash bucket determination module 601, an operation lock acquisition module 602, a data processing operation execution module 603, and an operation lock release module 604. The apparatus may optionally include at least one of an expansion module 605 and a rehashing module 606. The modules included in the apparatus are described below.

[0123] The hash value acquisition module 600 is mainly used to obtain the hash value corresponding to a piece of data to be processed and its processing method, based on the current hash function. The data to be processed includes a key or a key-value pair to be processed. The processing methods include read operations, write operations, or delete operations. The specific operations performed by the hash value acquisition module 600 can be found in the description of S200 in the above method embodiment, and will not be described in detail here.

[0124] The hash bucket determination module 601 is mainly used to determine the hash bucket to which the data to be processed belongs based on the hash value obtained by the hash value acquisition module 600. The specific operations performed by the hash bucket determination module 601 can be found in the description of S201 in the above method embodiments, and will not be described in detail here.

[0125] The obtaining operation lock module 602 is mainly configured to obtain an operation lock of the hash bucket determined by the hash bucket determination module 601 corresponding to the processing manner by using an atomic variable. The operation lock of the hash bucket can be a read lock of the hash bucket or a write lock of the hash bucket. For example, the obtaining operation lock module 602 can determine the expected original value in the CAS atomic variable. When the determination result is that the expected original value in the CAS atomic variable is not the first value, the obtaining operation lock module 602 changes the expected original value in the CAS atomic variable to the sum of the expected original value and a predetermined step length, thereby successfully obtaining the read lock of the hash bucket corresponding to the processing manner. When the determination result is that the expected original value in the CAS atomic variable is the first value, the obtaining operation lock module 602 again performs the step of determining the expected original value in the CAS atomic variable until the read lock of the hash bucket is obtained. When the determination result of the obtaining operation lock module 602 is that the expected original value in the CAS atomic variable is the second value, the obtaining operation lock module 602 changes the expected original value in the CAS atomic variable to the first value, thereby successfully obtaining the write lock of the hash bucket corresponding to the processing manner. When the determination result is that the expected original value in the CAS atomic variable is not the second value, the obtaining operation lock module 602 again performs the step of determining the expected original value in the CAS atomic variable until the write lock of the hash bucket is obtained. The first value indicates that the hash bucket is currently in a write lock state. The second value indicates that the hash bucket is currently in a lock-free state. The specific operations performed by the obtaining operation lock module 602 can be referred to the related description in the method embodiment for S202, Figure 3 and Figure 4 , which will not be described in detail here.

[0126] The execution data processing operation module 603 is mainly configured to, in the case that the operation lock acquisition module 602 successfully acquires the operation lock, perform a corresponding data processing operation on the data linked list in the hash bucket determined by the hash bucket determination module 601 according to the to-be-processed data and the processing manner thereof. For example, the execution data processing operation module 603 reads the value in the node matching the to-be-processed key from the data linked list in the hash bucket according to the to-be-processed key and the read operation. For another example, the execution data processing operation module 603 writes the to-be-processed key-value pair into the data linked list in the hash bucket according to the to-be-processed key-value pair and the write operation. For another example, the execution data processing operation module 603 modifies the value in the node matching the key in the to-be-processed key-value pair in the data linked list in the hash bucket to the value in the to-be-processed key-value pair according to the to-be-processed key-value pair and the write operation. For another example, the execution data processing operation module 603 deletes the node matching the to-be-processed key from the data linked list in the hash bucket according to the to-be-processed key and the delete operation. For another example, the execution data processing operation module 603 deletes the node matching the to-be-processed key-value pair from the data linked list in the hash bucket according to the to-be-processed key-value pair and the delete operation. Details of operations performed by the execution data processing operation module 603 can be referred to the description of S203 in the method embodiment, which will not be repeated here.

[0127] The release operation lock module 604 is mainly configured to, in the case that the execution data processing operation module 603 successfully performs the data processing operation, release the operation lock for the hash bucket acquired by the operation lock acquisition module 601. The process in which the release operation lock module 604 releases the operation lock is adapted to the process in which the operation lock acquisition module 601 acquires the operation lock. For example, the process in which the release operation lock module 604 releases the read lock can be: changing the expected original value in the CAS atomic variable to the difference between the expected original value and the predetermined step length. For another example, the process in which the release operation lock module 604 releases the write lock can be: changing the expected original value in the CAS atomic variable to the second value. Details of operations performed by the release operation lock module 604 can be referred to the description of S204 in the method embodiment, which will not be repeated here.

[0128] The expansion module 605 is mainly configured to, in the case that the number of key-value pairs stored in the HashMap meets the first predetermined condition and the operation lock acquisition module 602 successfully acquires the operation lock for the to-be-processed data and the processing manner thereof, migrate each key-value pair in the data linked list in the hash bucket to which the to-be-processed data belongs to a new HashMap. The storage space of the new HashMap is at least twice the storage space of the HashMap before migration, and the storage space of the new HashMap is 2 raised to the power of N, where N is an integer greater than 4. Details of operations performed by the expansion module 605 can be referred to the related description in the method embodiment, which will not be repeated here.

[0129] The rehashing module 606 is mainly used to select a candidate hash function from at least one candidate hash function to update the current hash function of the HashMap when the performance of the HashMap meets a second predetermined condition. It then uses atomic variables to acquire write locks for all hash buckets in the HashMap. After successfully acquiring write locks for all hash buckets, the rehashing module 606 rehashes all key-value pairs in the HashMap according to the updated current hash function. Specifically, the rehashing module 606 selects a candidate hash function by performing hash calculations on the keys of each key-value pair in the data linked lists of at least some hash buckets in the HashMap using each candidate hash function. Based on the hash calculation results of each candidate hash function, it determines the performance evaluation result of each candidate hash function. Finally, the rehashing module 606 selects a candidate hash function to update the current hash function of the HashMap based on the performance evaluation results of each candidate hash function. The specific operations performed by the rehashing module 606 can be found in the relevant descriptions in the above method embodiments, and will not be detailed here.

[0130] Example electronic device

[0131] The following is for reference. Figure 7 To describe an electronic device according to embodiments of the present disclosure. Figure 7 A block diagram of an electronic device according to an embodiment of the present disclosure is shown. (As follows) Figure 7 As shown, the electronic device 71 includes one or more processors 711 and memory 712.

[0132] The processor 711 may be a central processing unit (CPU) or other form of processing unit with data processing capabilities and / or instruction execution capabilities, and may control other components in the electronic device 71 to perform desired functions.

[0133] The memory 712 may include one or more computer program products, which may include various forms of computer-readable storage media, such as volatile memory and / or non-volatile memory. The volatile memory may, for example, include random access memory (RAM) and / or cache memory. The non-volatile memory may, for example, include read-only memory (ROM), hard disk, and flash memory. One or more computer program instructions may be stored on the computer-readable storage medium, and the processor 711 may execute the program instructions to implement the concurrent HashMap implementation methods of the various embodiments of this disclosure described above, and / or other desired functions.

[0134] In one example, the electronic device 71 can further include an input device 713, an output device 714, and the like, which are interconnected through a bus system and / or other forms of connection mechanisms (not shown). In addition, the input device 713 can include, for example, a keyboard, a mouse, and the like. The output device 714 can output various information to the outside. The output device 714 can include, for example, a display, a speaker, a printer, a communication network and a remote output device connected thereto, and the like.

[0135] Of course, in order to simplify, Figure 7 In the electronic device 71, only some of the components related to the present disclosure are shown, and components such as buses, input / output interfaces, and the like are omitted. In addition, the electronic device 71 can include any other appropriate components according to the specific application.

[0136] Example computer program product and computer readable storage medium

[0137] In addition to the above-mentioned methods and devices, embodiments of the present disclosure can also be a computer program product including computer program instructions, which, when executed by a processor, cause the processor to perform the steps of the method for supporting concurrent HashMap implementation according to various embodiments of the present disclosure described in the above "Exemplary Method" section of the present specification.

[0138] The computer program product can be written in any combination of one or more programming languages, including an object-oriented programming language such as Java, C++, and the like, and conventional procedural programming languages, such as the "C" programming language, or the like. The program code can execute entirely on the user's computing device, partly on the user's device, as a stand-alone software package, partly on the user's computing device and partly on a remote computing device, or entirely on the remote computing device or server.

[0139] In addition, embodiments of the present disclosure can also be a computer readable storage medium having stored thereon computer program instructions, which, when executed by a processor, cause the processor to perform the steps of the method for supporting concurrent HashMap implementation according to various embodiments of the present disclosure described in the above "Exemplary Method" section of the present specification.

[0140] The computer readable storage medium can be any combination of one or more computer readable medium(s). The computer readable medium can be a computer readable signal medium or a computer readable storage medium. A computer readable storage medium can be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any suitable combination of the foregoing. More specific examples (a non-exhaustive list) of the computer readable storage medium include an electrical connection having one or more wires, a portable disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or Flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.

[0141] The above description has been presented to describe the basic principles of the disclosure, but it should be pointed out that the advantages, advantages, effects and the like mentioned in the disclosure are only examples and are not limiting, and these advantages, advantages and effects and the like cannot be considered as the various embodiments of the disclosure must have. In addition, the above specific details of the disclosure are only for the purpose of example and for the purpose of understanding, and are not limited to the above specific details, and the above specific details do not limit the disclosure to be necessarily implemented.

[0142] Each embodiment in the specification is described in a progressive manner, and each embodiment focuses on the difference from other embodiments. The same or similar parts between each embodiment can be referred to each other. For system embodiments, since they basically correspond to method embodiments, the description is relatively simple, and the relevant parts can be referred to the part of the method embodiment.

[0143] The methods and apparatuses of the disclosure can be implemented in many ways. For example, the methods and apparatuses of the disclosure can be implemented in software, hardware, firmware, or any combination of software, hardware, and firmware. The above order of the steps of the method is only for illustration, and the steps of the method of the disclosure are not limited to the above specific description. Unless otherwise specifically stated, the order of the steps of the method of the disclosure is not limited to the above specific description. In addition, in some embodiments, the disclosure can also be implemented as a program recorded in a recording medium, which includes machine readable instructions for implementing the method according to the disclosure. Therefore, the disclosure also covers the recording medium storing the program for executing the method according to the disclosure.

[0144] The above description has been given for the purpose of illustration and description. In addition, this description is not intended to limit the embodiments of the disclosure to the forms disclosed herein. Although the above has discussed a plurality of example aspects and embodiments, those skilled in the art will recognize certain variations, modifications, changes, additions and sub-combinations.

Claims

1. A concurrent HashMap implementation method, characterized in that, include: For a given set of data to be processed and its processing method, the hash value corresponding to the data to be processed is obtained according to the current hash function; wherein, the data to be processed includes: a key to be processed or a key-value pair to be processed; the processing method includes: a read operation, a write operation, or a delete operation; Based on the hash value, determine the hash bucket to which the data to be processed belongs; Using atomic variables, obtain the operation lock for the hash bucket corresponding to the processing method; wherein, the operation lock for the hash bucket includes: a read lock for the hash bucket or a write lock for the hash bucket; If the operation lock is successfully acquired, the corresponding data processing operation is performed on the data linked list in the hash bucket according to the data to be processed and its processing method. If the data processing operation is successfully executed, the acquired operation lock for the hash bucket is released.

2. The method according to claim 1, characterized in that, The step of using atomic variables to obtain the operation lock for the hash bucket corresponding to the processing method includes: Determine, compare, and swap the expected original values ​​in the CAS atomic variables; When the judgment result is that the expected original value in the CAS atomic variable is not the first value, the expected original value in the CAS atomic variable is changed to the sum of the expected original value and the predetermined step size, thereby successfully obtaining the read lock for the hash bucket corresponding to the processing method; If the judgment result is that the expected original value of the CAS atomic variable is the first value, the step of judging the expected original value of the CAS atomic variable is executed again; The first value indicates that the hash bucket is currently in a write lock state; Furthermore, releasing the operation lock obtained above for the hash bucket includes: Change the expected original value in the CAS atomic variable to the difference between the expected original value and the predetermined step size.

3. The method according to claim 1, characterized in that, The step of using atomic variables to obtain the operation lock for the hash bucket corresponding to the processing method includes: Determine the expected original value in the CAS atomic variable; When the judgment result is that the expected original value in the CAS atomic variable is the second value, the expected original value in the CAS atomic variable is changed to the first value, thereby successfully obtaining the write lock for the hash bucket corresponding to the processing method; If the judgment result is that the expected original value in the CAS atomic variable is not the second value, the step of judging the expected original value of the CAS atomic variable is executed again; The second value indicates that the hash bucket is currently in an unlocked state; Furthermore, releasing the operation lock obtained above for the hash bucket includes: Change the expected original value in the CAS atomic variable to the second value.

4. The method according to claim 1, characterized in that, The step of performing corresponding data processing operations on the data linked list in the hash bucket according to the data to be processed and its processing method includes: Based on the key to be processed and its read operation, read the value from the node matching the key in the data linked list in the hash bucket; or Based on the key-value pair to be processed and its write operation, write the key-value pair to be processed into the data linked list in the hash bucket; or Based on the key-value pair to be processed and its write operation, modify the value in the node in the data linked list of the hash bucket that matches the key in the key-value pair to be processed to the value in the key-value pair to be processed; or Based on the key to be processed and its deletion operation, delete the node matching the key from the data linked list in the hash bucket; or Based on the key-value pair to be processed and its deletion operation, delete the node that matches the key-value pair to be processed from the data linked list in the hash bucket.

5. The method according to any one of claims 1 to 4, characterized in that, The method further includes: If the number of key-value pairs stored in the HashMap meets the first predetermined condition, and the operation lock is successfully acquired for the data to be processed and its processing method, the key-value pairs in the data linked list of the hash bucket to which the data to be processed belongs are migrated to the new HashMap. The storage space of the new HashMap is at least twice that of the original HashMap, and the storage space of the new HashMap is a power of 2, where N is an integer greater than 4.

6. The method according to any one of claims 1 to 4, characterized in that, The method further includes: If the performance of the HashMap is found to meet the second predetermined condition, a candidate hash function is selected from at least one candidate hash function to update the current hash function; Use atomic variables to acquire write locks for all hash buckets in the HashMap; If write locks are successfully acquired for all hash buckets, all key-value pairs in the HashMap are rehashed according to the updated current hash function.

7. The method according to claim 6, characterized in that, The step of selecting a candidate hash function from at least one candidate hash function to update the current hash function includes: The key in each key-value pair in the data linked list of at least some hash buckets in the HashMap is hashed using each candidate hash function; Based on the hash calculation results corresponding to each candidate hash function, determine the performance evaluation results of each candidate hash function; Based on the performance evaluation results of each candidate hash function, a candidate hash function is selected to update the current hash function.

8. A concurrent HashMap implementation device, characterized in that, include: The hash value acquisition module is used to acquire the hash value corresponding to a piece of data to be processed and its processing method according to the current hash function; wherein, the data to be processed includes: a key to be processed or a key-value pair to be processed; the processing method includes: a read operation, a write operation, or a delete operation; The hash bucket determination module is used to determine the hash bucket to which the data to be processed belongs based on the hash value obtained by the hash value acquisition module. An operation lock acquisition module is used to acquire the operation lock for the hash bucket corresponding to the processing method using atomic variables; wherein, the operation lock for the hash bucket includes: a read lock for the hash bucket or a write lock for the hash bucket; The data processing operation module is used to perform corresponding data processing operations on the data linked list in the hash bucket determined by the hash bucket determination module, based on the data to be processed and its processing method, when the operation lock acquisition module successfully acquires the operation lock. The operation lock release module is used to release the operation lock for the hash bucket acquired by the operation lock acquisition module when the data processing operation module successfully executes the data processing operation.

9. A computer-readable storage medium storing a computer program for performing the method according to any one of claims 1-7.

10. An electronic device, the electronic device comprising: processor; Memory used to store the processor's executable instructions; The processor is configured to read the executable instructions from the memory and execute the instructions to implement the method of any one of claims 1-7.

Citation Information

Patent Citations

  • Hash caching method and device

    CN113297101A

  • Database row lock processing method and device, equipment and storage medium

    CN114064297A

  • Flow table management method and device, electronic equipment and storage medium

    CN115065640A

  • Data sharing method and device, equipment and storage medium

    CN117891625A

  • Lock acceleration

    US20130185270A1