当前位置:网站首页>ConcurrentSkipListMap——跳表原理
ConcurrentSkipListMap——跳表原理
2022-07-01 23:17:00 【HD243608836】
为了引出 ConcurrentSkipListMap,先来简单理解下什么是跳表。
对于单链表,即使链表是有序的,如果想要在其中查找某个数据,也只能从头到尾遍历链表,这样效率自然就会很低,跳表就不一样了。跳表是一种可以用来快速查找的数据结构,有点类似于平衡树。它们都可以对元素进行快速的查找。但一个重要的区别是:对平衡树的插入和删除往往很可能导致平衡树进行一次全局的调整;而对跳表的插入和删除,只需要对整个数据结构的局部进行操作即可。这样带来的好处是:在高并发的情况下,需要一个全局锁,来保证整个平衡树的线程安全;而对于跳表,则只需要部分锁即可。这样,在高并发环境下,就可以拥有更好的性能。就查询的性能而言,跳表的时间复杂度是 O(logn), 所以在并发数据结构中,JDK 使用跳表来实现一个 Map。
跳表的本质,是同时维护了多个链表,并且链表是分层的:

最低层的链表,维护了跳表内所有的元素,每上面一层链表,都是下面一层的子集。
跳表内所有链表的元素都是排序的。查找时,可以从顶级链表开始找。一旦发现被查找的元素大于当前链表中的取值,就会转入下一层链表继续找。这也就是说在查找过程中,搜索是跳跃式的。如上图所示,在跳表中查找元素18。

查找18 的时候,原来需要遍历 18 次,现在只需要 7 次即可。针对链表长度比较大的时候,构建索引,查找效率的提升就会非常明显。
从上面很容易看出,跳表是一种利用空间换时间的算法。
使用跳表实现 Map,和使用哈希算法实现 Map 的另外一个不同之处是:哈希并不会保存元素的顺序,而跳表内所有的元素都是排序的。因此,在对跳表进行遍历时,你会得到一个有序的结果。所以,如果你的应用需要有序性,那么跳表就是你不二的选择。
JDK 中实现这一数据结构的类是 ConcurrentSkipListMap
。
边栏推荐
- VIM color the catalogue
- dat. GUI
- MT manager test skiing Adventure
- Redis master-slave synchronization
- 13 MySQL-约束
- De PIP. Interne. CLI. Main Import main modulenotfounderror: No module named 'PIP'
- Development trend and future direction of neural network Internet of things
- The online beggar function of Japanese shopping websites
- What is mosaic?
- Is there a piece of code that makes you convinced by human wisdom
猜你喜欢
ARP message header format and request flow
2022年起重机司机(限桥式起重机)考试试题及模拟考试
CKS CKA ckad change terminal to remote desktop
Wechat personal small store one click opening assistant applet development
flutter Unable to load asset: assets/images/888. png
2021 RoboCom 世界机器人开发者大赛-本科组初赛
Redis AOF日志
Current situation and future development trend of Internet of things
2022 R1 fast opening pressure vessel operation test questions and answers
深度学习 | 三个概念:Epoch, Batch, Iteration
随机推荐
[micro service sentinel] sentinel integrates openfeign
sql 优化
CKS CKA ckad change terminal to remote desktop
纪念成为首个DAYUs200三方demo贡献者
软件架构的本质
Redis master-slave synchronization
JS - use of arguments
Postgresql源码(58)元组拼接heap_form_tuple剖析
Win 10 mstsc connect RemoteApp
Redis RDB快照
学成在线案例实战
Practical application and extension of plain framework
Leetcode(34)——在排序数组中查找元素的第一个和最后一个位置
Redis data types and application scenarios
Y53. Chapter III kubernetes from introduction to mastery -- ingress (26)
股票开户哪个证券公司最好,有安全保障吗
有没有一段代码,让你为人类的智慧所折服
MT manager test skiing Adventure
Commemorate becoming the first dayus200 tripartite demo contributor
2022 examination questions and online simulation examination for safety management personnel of hazardous chemical business units