SDK 内部的跳表实现 —— ConcurrentSkipListMap

主题

SDK 内部的跳表实现 —— ConcurrentSkipListMap

JavaDoc

A scalable concurrent ConcurrentNavigableMapnatural orderingComparator

This class implements a concurrent variant of SkipLists providing expected average log(n) time cost for the containsKey, get, put and remove 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 the Entry.setValue method. (Note however that it is possible to change mappings in the associated map using put, putIfAbsent, or replace, 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 operations putAll, equals, toArray, containsValue, and clear are not guaranteed to be performed atomically. For example, an iterator operating concurrently with a putAll 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 and Iterator interfaces. Like most other concurrent collections, this class does not permit the use of null 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

说人话就是:

  1. 一个可伸缩,支持并发,可找到最接近的数据,按自然顺序排序的实现。(另一个是 TreeMap,红黑树实现。我们昨天说了,跳表实现了红黑树的功能,但是比红黑树更简单,更容易理解,主要使用随机,也就是概率。)
  2. 这是跳表的实现。
  3. 迭代器和可分割迭代器都是保证弱持久性的。
  4. 顺序访问比逆序访问快。
  5. Entry 对象是一个快照,保存着产生时的信息。不能通过 setValue 改。但是其他方式仍然可以改。
  6. size() 不是一个固定时间返回的值,随着容量变化而变,因为需要遍历得到。
  7. 行为类似普通的 MapIterator,也不支持 null
  8. Java Collections Framework 成员。
  9. JDK 1.6 就开始有了。

骚操作

其实所谓的骚就是刚才说的“可找到最接近的数据”。

ConcurrentSkipListMap<Integer, Integer> map = new ConcurrentSkipListMap<>();
for(int i = 0; i < 700; i+=7){
map.put(i, ThreadLocalRandom.current().nextInt())
}
Map.Entry<Integer, Integer> entry1 = map.lowerEntry(43);
Map.Entry<Integer, Integer> entry2 = map.higherEntry(43);
Map.Entry<Integer, Integer> entry3 = map.floorEntry(42);
Map.Entry<Integer, Integer> entry4 = map.ceilingEntry(42);
LOGGER.info("entry: {}", entry1);
LOGGER.info("entry: {}", entry2);
LOGGER.info("entry: {}", entry3);
LOGGER.info("entry: {}", entry4);

输出:

12:44:49.950 [INFO ][main][skiplist.SkipListMapTest] - lower entry: 42=508217011
12:44:49.959 [INFO ][main][skiplist.SkipListMapTest] - higher entry: 49=-1679895809
12:44:49.960 [INFO ][main][skiplist.SkipListMapTest] - floor entry: 42=508217011
12:44:49.960 [INFO ][main][skiplist.SkipListMapTest] - ceiling entry: 42=508217011

四个方法均是望文生义,就不解释了。