ConcurrentSikpListMap:
有序
采用cas无锁机制保证多线程竞态条件;死循环+volatile(as if serial内存屏障)+cas(compareAndSwap)
时间复杂度(假设是平分,总的个数是n,在真实的代码是随机产生level。哈哈复杂度由复杂了):
第一层 0到n;
第二层 0到n的一半,n的一半到n
第三层 0到n的四分之一。类比n/2的k次方=1,logn
空间复杂度(同样假设,由下往上,跟上面的反过来)
第一层,大家都存,个个都在
第二层,一半
第三层,四分之一 多了n的一半,n的四分之一,累加n-2
明显用空间换时间
java.util.concurrent.ConcurrentSkipListMap concurrentSkipListMap = new java.util.concurrent.ConcurrentSkipListMap();
concurrentSkipListMap.put(5, 0);
concurrentSkipListMap.put(3, 3);
concurrentSkipListMap.put(4, "a");
java.util.Iterator o = concurrentSkipListMap.entrySet().iterator();
while(o.hasNext()){
System.out.println(o.next());
}
数据结构:节点及索引节点
节点node
static final class Node<K,V> {
final K key;
volatile Object value;
volatile Node<K,V> next;
Node(K key, Object value, Node<K,V> next) {
this.key = key;
this.value = value;
this.next = next;
}
}
索引节点:本身是node,但是加上了指向下面节点和后面节点的索引;
static class Index<K,V> {
final Node<K,V> node;
final Index<K,V> down;
volatile Index<K,V> right;
Index(Node<K,V> node, Index<K,V> down, Index<K,V> right) {
this.node = node;
this.down = down;
this.right = right;
}
}
有空分析,读读put;构建node。
doPut(K, V, boolean)