标签:blog   http   io   os   使用   java   ar   strong   for   
一、前言 
concurrentHashMap与ConcurrentSkipListMap性能测试
在4线程1.6万数据的条件下,ConcurrentHashMap 存取速度是ConcurrentSkipListMap 的4倍左右。
但ConcurrentSkipListMap有几个ConcurrentHashMap 不能比拟的优点:
1、ConcurrentSkipListMap 的key是有序的。
2、ConcurrentSkipListMap 支持更高的并发。ConcurrentSkipListMap 的存取时间是log(N),和线程数几乎无关。也就是说在数据量一定的情况下,并发的线程越多,ConcurrentSkipListMap越能体现出他的优势。 
 
二、使用建议
在非多线程的情况下,应当尽量使用TreeMap。此外对于并发性相对较低的并行程序可以使用Collections.synchronizedSortedMap将TreeMap进行包装,也可以提供较好的效率。对于高并发程序,应当使用ConcurrentSkipListMap,能够提供更高的并发度。
所以在多线程程序中,如果需要对Map的键值进行排序时,请尽量使用ConcurrentSkipListMap,可能得到更好的并发度。
注意,调用ConcurrentSkipListMap的size时,由于多个线程可以同时对映射表进行操作,所以映射表需要遍历整个链表才能返回元素个数,这个操作是个O(log(n))的操作。
 
二、什么是SkipList
 
Skip list(跳表)是一种可以代替平衡树的数据结构,默认是按照Key值升序的。Skip list让已排序的数据分布在多层链表中,以0-1随机数决定一个数据的向上攀升与否,通过“空间来换取时间”的一个算法,在每个节点中增加了向前的指针,在插入、删除、查找时可以忽略一些不可能涉及到的结点,从而提高了效率。
从概率上保持数据结构的平衡比显示的保持数据结构平衡要简单的多。对于大多数应用,用Skip list要比用树算法相对简单。由于Skip list比较简单,实现起来会比较容易,虽然和平衡树有着相同的时间复杂度(O(logn)),但是skip list的常数项会相对小很多。Skip list在空间上也比较节省。一个节点平均只需要1.333个指针(甚至更少)。
                
图1-1 Skip list结构图(以7,14,21,32,37,71,85序列为例)
 
Skip list的性质
(1) 由很多层结构组成,level是通过一定的概率随机产生的。
(2) 每一层都是一个有序的链表,默认是升序,也可以根据创建映射时所提供的Comparator进行排序,具体取决于使用的构造方法。
(3) 最底层(Level 1)的链表包含所有元素。
(4) 如果一个元素出现在Level i 的链表中,则它在Level i 之下的链表也都会出现。
(5) 每个节点包含两个指针,一个指向同一链表中的下一个元素,一个指向下面一层的元素。
 
三、什么是ConcurrentSkipListMap
 
ConcurrentSkipListMap提供了一种线程安全的并发访问的排序映射表。内部是SkipList(跳表)结构实现,在理论上能够在O(log(n))时间内完成查找、插入、删除操作。
       注意,调用ConcurrentSkipListMap的size时,由于多个线程可以同时对映射表进行操作,所以映射表需要遍历整个链表才能返回元素个数,这个操作是个O(log(n))的操作。
 
 ConcurrentSkipListMap存储结构
 

ConcurrentSkipListMap存储结构图
 
跳跃表(SkipList):(如上图所示)
1.多条链构成,是关键字升序排列的数据结构;
2.包含多个级别,一个head引用指向最高的级别,最低(底部)的级别,包含所有的key;
3.每一个级别都是其更低级别的子集,并且是有序的;
4.如果关键字 key在 级别level=i中出现,则,level<=i的链表中都会包含该关键字key;
 
------------------------
ConcurrentSkipListMap主要用到了Node和Index两种节点的存储方式,通过volatile关键字实现了并发的操作
  
- static final class Node<K,V> {  
-         final K key;  
-         volatile Object value;
-         volatile Node<K,V> next;
-         ……  
- }  
- static class Index<K,V> {  
-         final Node<K,V> node;  
-         final Index<K,V> down;
-        volatile Index<K,V> right;
-        ……  
- }  
 
 
 
------------------------
ConcurrentSkipListMap的查找
 
通过SkipList的方式进行查找操作:(下图以“查找91”进行说明:)
 

红色虚线,表示查找的路径,蓝色向右箭头表示right引用;黑色向下箭头表示down引用;
 
/get方法,通过doGet操作实现
 
- public V get(Object key) {  
-       return doGet(key);  
-  }  
-  
- private V doGet(Object okey) {  
-         Comparable<? super K> key = comparable(okey);  
-         Node<K,V> bound = null;  
-         Index<K,V> q = head;
-         Index<K,V> r = q.right;
-         Node<K,V> n;  
-         K k;  
-         int c;  
-         for (;;) {
-             Index<K,V> d;  
-             
-             if (r != null && (n = r.node) != bound && (k = n.key) != null) {  
-                 if ((c = key.compareTo(k)) > 0) {
-                     q = r;  
-                     r = r.right;  
-                     continue;  
-                 } else if (c == 0) {  
-                     
-                     
-                     Object v = n.value;  
-                     return (v != null)? (V)v : getUsingFindNode(key);  
-                 } else  
-                     bound = n;  
-             }  
-             
-             if ((d = q.down) != null) {  
-                 q = d;  
-                 r = d.right;  
-             } else  
-                 break;  
-         }  
-         
-         for (n = q.node.next;  n != null; n = n.next) {  
-             if ((k = n.key) != null) {  
-                 if ((c = key.compareTo(k)) == 0) {  
-                     Object v = n.value;  
-                     return (v != null)? (V)v : getUsingFindNode(key);  
-                 } else if (c < 0)  
-                     break;  
-             }  
-         }  
-         return null;  
-     }  
 
 
 
 
------------------------------------------------
ConcurrentSkipListMap的删除
 
通过SkipList的方式进行删除操作:(下图以“删除23”进行说明:)
 

红色虚线,表示查找的路径,蓝色向右箭头表示right引用;黑色向下箭头表示down引用;
 
 
- public V remove(Object key) {  
-         return doRemove(key, null);  
-  }  
-  final V doRemove(Object okey, Object value) {  
-         Comparable<? super K> key = comparable(okey);  
-         for (;;) {  
-             Node<K,V> b = findPredecessor(key);
-             Node<K,V> n = b.next;
-             for (;;) {
-                 if (n == null)
-                     return null;  
-                 Node<K,V> f = n.next;  
-                 if (n != b.next)                    
-                     break;  
-                 Object v = n.value;  
-                 if (v == null) {                    
-                     n.helpDelete(b, f);  
-                     break;  
-                 }  
-                 if (v == n || b.value == null)      
-                     break;  
-                 int c = key.compareTo(n.key);  
-                 if (c < 0)  
-                     return null;  
-                 if (c > 0) {
-                     b = n;  
-                     n = f;  
-                     continue;  
-                 }  
-                 if (value != null && !value.equals(v))  
-                     return null;  
-                 if (!n.casValue(v, null))  
-                     break;  
-                 if (!n.appendMarker(f) || !b.casNext(n, f))
-                     findNode(key);                  
-                 else {  
-                     findPredecessor(key);           
-                     if (head.right == null)   
-                         tryReduceLevel();  
-                 }  
-                 return (V)v;  
-             }  
-         }  
-     }  
 
 
-------------------------------------
 
ConcurrentSkipListMap的插入
 
 
通过SkipList的方式进行插入操作:(下图以“添加55”的两种情况,进行说明:)

在level=2(该level存在)的情况下添加55的图示:只需在level<=2的合适位置插入55即可
--------

在level=4(该level不存在,图示level4是新建的)的情况下添加55的情况:首先新建level4,然后在level<=4的合适位置插入55
-----------
 
-  public V put(K key, V value) {  
-         if (value == null)  
-             throw new NullPointerException();  
-         return doPut(key, value, false);  
-  }  
- private V doPut(K kkey, V value, boolean onlyIfAbsent) {  
-         Comparable<? super K> key = comparable(kkey);  
-         for (;;) {  
-             Node<K,V> b = findPredecessor(key);
-             Node<K,V> n = b.next;  
-            
-             for (;;) {  
-                 if (n != null) {  
-                     Node<K,V> f = n.next;  
-                     if (n != b.next)               
-                         break;;  
-                     Object v = n.value;  
-                     if (v == null) {               
-                         n.helpDelete(b, f);  
-                         break;  
-                     }  
-                     if (v == n || b.value == null) 
-                         break;  
-                     int c = key.compareTo(n.key);  
-                     if (c > 0) {  
-                         b = n;  
-                         n = f;  
-                         continue;  
-                     }  
-                     if (c == 0) {  
-                         if (onlyIfAbsent || n.casValue(v, value))  
-                             return (V)v;  
-                         else  
-                             break; 
-                     }  
-                     
-                 }  
-                 Node<K,V> z = new Node<K,V>(kkey, value, n);  
-                 if (!b.casNext(n, z))  
-                     break;         
-                 int level = randomLevel();
-                 if (level > 0)  
-                     insertIndex(z, level);
-                 return null;  
-             }  
-         }  
-     }  
-   
-  
-     private int randomLevel() {  
-         int x = randomSeed;  
-         x ^= x << 13;  
-         x ^= x >>> 17;  
-         randomSeed = x ^= x << 5;  
-         if ((x & 0x8001) != 0) 
-             return 0;  
-         int level = 1;  
-         while (((x >>>= 1) & 1) != 0) ++level;  
-         return level;  
-     }  
- private void insertIndex(Node<K,V> z, int level) {  
-         HeadIndex<K,V> h = head;  
-         int max = h.level;  
-         if (level <= max) {
-             Index<K,V> idx = null;  
-             for (int i = 1; i <= level; ++i)
-                 idx = new Index<K,V>(z, idx, null);  
-             addIndex(idx, h, level);
-         } else { 
-             level = max + 1;  
-             Index<K,V>[] idxs = (Index<K,V>[])new Index[level+1];  
-             Index<K,V> idx = null;  
-             for (int i = 1; i <= level; ++i)
-                 idxs[i] = idx = new Index<K,V>(z, idx, null);  
-             HeadIndex<K,V> oldh;  
-             int k;  
-             for (;;) {  
-                 oldh = head;  
-                 int oldLevel = oldh.level;  
-                 if (level <= oldLevel) { 
-                     k = level;  
-                     break;  
-                 }  
-                 HeadIndex<K,V> newh = oldh;  
-                 Node<K,V> oldbase = oldh.node;  
-                 for (int j = oldLevel+1; j <= level; ++j)  
-                     newh = new HeadIndex<K,V>(oldbase, newh, idxs[j], j);
-                 if (casHead(oldh, newh)) {  
-                     k = oldLevel;  
-                     break;  
-                 }  
-             }  
-             addIndex(idxs[k], oldh, k);  
-         }  
-     }  
-     private void addIndex(Index<K,V> idx, HeadIndex<K,V> h, int indexLevel) {  
-         
-         int insertionLevel = indexLevel;  
-         Comparable<? super K> key = comparable(idx.node.key);  
-         if (key == null) throw new NullPointerException();  
-         
-         for (;;) {  
-             int j = h.level;  
-             Index<K,V> q = h;  
-             Index<K,V> r = q.right;  
-             Index<K,V> t = idx;  
-             for (;;) {  
-                 if (r != null) {  
-                     Node<K,V> n = r.node;  
-                     
-                     int c = key.compareTo(n.key);  
-                     if (n.value == null) {  
-                         if (!q.unlink(r))  
-                             break;  
-                         r = q.right;  
-                         continue;  
-                     }  
-                     if (c > 0) {  
-                         q = r;  
-                         r = r.right;  
-                         continue;  
-                     }  
-                 }  
-                 if (j == insertionLevel) {
-                     
-                     if (t.indexesDeletedNode()) {  
-                         findNode(key); 
-                         return;  
-                     }  
-                     if (!q.link(r, t))
-                         break; 
-                     if (--insertionLevel == 0) {  
-                         
-                         if (t.indexesDeletedNode())  
-                             findNode(key);  
-                         return;  
-                     }  
-                 }  
-                 if (--j >= insertionLevel && j < indexLevel)
-                     t = t.down;  
-                 q = q.down;  
-                 r = q.right;  
-             }  
-         }  
-     }  
 
参考:
 
集合框架 Map篇(5)----ConcurrentSkipListMap http://hi.baidu.com/yao1111yao/item/0f3008163c4b82c938cb306d
Java里多个Map的性能比较(TreeMap、HashMap、ConcurrentSkipListMap) http://blog.hongtium.com/java-map-skiplist/
跳表SkipList的原理和实现 http://imtinx.iteye.com/blog/1291165 
Java多线程(四)之ConcurrentSkipListMap深入分析
标签:blog   http   io   os   使用   java   ar   strong   for   
原文地址:http://www.cnblogs.com/xiaorenwu702/p/3974701.html