SDK 内部的跳表实现 —— ConcurrentSkipListMap
主题
SDK
内部的跳表实现 —— ConcurrentSkipListMap
JavaDoc
A scalable concurrent
ConcurrentNavigableMap
natural ordering
Comparator
This class implements a concurrent variant of
SkipLists
providing expected average log(n) time cost for thecontainsKey
,get
,put
andremove
operations and their variants. Insertion, removal, update, and access operations safely execute concurrently by multiple threads.Iterators and spliterators are
weakly consistent
.Ascending key ordered views and their iterators are faster than descending ones.
All
Map.Entry
pairs returned by methods in this class and its views represent snapshots of mappings at the time they were produced. They do not support theEntry.setValue
method. (Note however that it is possible to change mappings in the associated map usingput
,putIfAbsent
, orreplace
, depending on exactly which effect you need.)Beware that, unlike in most collections, the
size
method is not a constant-time operation. Because of the asynchronous nature of these maps, determining the current number of elements requires a traversal of the elements, and so may report inaccurate results if this collection is modified during traversal. Additionally, the bulk operationsputAll
,equals
,toArray
,containsValue
, andclear
are not guaranteed to be performed atomically. For example, an iterator operating concurrently with aputAll
operation might view only some of the added elements.This class and its views and iterators implement all of the optional methods of the
Map
andIterator
interfaces. Like most other concurrent collections, this class does not permit the use ofnull
keys or values because some null return values cannot be reliably distinguished from the absence of elements.This class is a member of the Java Collections Framework.
Since:
1.6
说人话就是:
- 一个可伸缩,支持并发,可找到最接近的数据,按自然顺序排序的实现。(另一个是
TreeMap
,红黑树实现。我们昨天说了,跳表实现了红黑树的功能,但是比红黑树更简单,更容易理解,主要使用随机,也就是概率。) - 这是跳表的实现。
- 迭代器和可分割迭代器都是保证弱持久性的。
- 顺序访问比逆序访问快。
Entry
对象是一个快照,保存着产生时的信息。不能通过setValue
改。但是其他方式仍然可以改。size()
不是一个固定时间返回的值,随着容量变化而变,因为需要遍历得到。- 行为类似普通的
Map
和Iterator
,也不支持null
。 - 是
Java Collections Framework
成员。 JDK 1.6
就开始有了。
骚操作
其实所谓的骚就是刚才说的“可找到最接近的数据”。
ConcurrentSkipListMap<Integer, Integer> map = new ConcurrentSkipListMap<>(); |
输出:
12:44:49.950 [INFO ][main][skiplist.SkipListMapTest] - lower entry: 42=508217011 |
四个方法均是望文生义,就不解释了。