摘要
HashMap内存数据结构存在相当广泛的应用场景,通过Hash函数的Key直接获取对应的值,能够确保搜索的时间复杂度为O(1)。HashMap数据结构存在哈希冲突与线程安全问题,悲观锁机制实现线程安全的方法存在很大的性能开销。本文提出了基于优化的CAS算法,实现线程安全的哈希映射数据结构,内部采用数组、链表和红黑树实现了高并发环境下读写操作。通过增加版本戳避免CAS算法的ABA问题,CAS算法实现的无锁方式避免了锁竞争的开销,使用红黑树来优化链表,确保大规模数据集的检索时间复杂度保持O(logn)。支持多线程扩容操作,在执行效率方面有良好的表现。通过大规模的并发压力测试,验证了该数据结构在性能上有稳定的提升。
HashMap data structure is widely used in the industry. The Hash function directly reads the corresponding value through the input key, which ensures that the time complexity of the search algorithm is O(1). There are hash conflicts and thread safety issues in the HashMap. The pessimistic locking mechanism implements thread safety methods with great performance overhead. This paper proposes a HashMap structure based on optimized CAS algorithm to implement thread safety. The internal use of arrays, linked lists, red-black trees. Achieves read and write efficiency in high-concurrency environments. The CAS algorithm implements lock-free mode to avoid the overhead of lock competition. Red-black trees are used to optimize the linked list, ensuring that the time complexity remains O(logn) when the data set is large. Supports multi-thread expansion operations in a lock-free state, and concurrent processing reduces the time overhead of capacity expansion. Through large-scale concurrent testing, it is verified that the data structure has a stable improvement in performance.
作者
吴恩慈
WU En-ci(Shanghai Qiyu Information Technology Co., Ltd., Shanghai 200120)
出处
《软件》
2019年第6期185-190,共6页
Software