为什么将ConcurrentNavigableMap实现为跳过列表?

时间:2012-10-11 13:58:34

标签: java data-structures skip-lists

最近我遇到了ConcurrentSkipListMap,这是skip list ConcurrentNavigableMap的实施。现在我想知道,为什么它被实现为skip list

skip list并发可导航地图的“标准”实施吗?是什么让skip list特别有用?

1 个答案:

答案 0 :(得分:5)

ConcurrentSkipListMap源代码记录了原因。

采用这种方法而不是通常的基于数组的

有两个原因
  1. 基于数组的实现似乎遇到了更多的复杂性 开销
  2. 我们可以为频繁遍历的索引列表使用更便宜的算法 可以用于基本列表
  3. 这是javadoc

     /*
     * This class implements a tree-like two-dimensionally linked skip
     * list in which the index levels are represented in separate
     * nodes from the base nodes holding data.  **There are two reasons
     * for taking this approach instead of the usual array-based**
     * structure: 1) Array based implementations seem to encounter
     * more complexity and overhead 2) We can use cheaper algorithms
     * for the heavily-traversed index lists than can be used for the
     * base lists.  Here's a picture of some of the basics for a
     * possible list with 2 levels of index:
     *
     * Head nodes          Index nodes
     * +-+    right        +-+                      +-+
     * |2|---------------->| |--------------------->| |->null
     * +-+                 +-+                      +-+
     *  | down              |                        |
     *  v                   v                        v
     * +-+            +-+  +-+       +-+            +-+       +-+
     * |1|----------->| |->| |------>| |----------->| |------>| |->null
     * +-+            +-+  +-+       +-+            +-+       +-+
     *  v              |    |         |              |         |
     * Nodes  next     v    v         v              v         v
     * +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+
     * | |->|A|->|B|->|C|->|D|->|E|->|F|->|G|->|H|->|I|->|J|->|K|->null
     * +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+