java并发容器分析

简介

    JDK5中添加了新的concurrent包,其中包含了很多并发容器,这些容器针对多线程环境进行了优化,大大提高了容器类在并发环境下的执行效率。

    CopyOnWriteArrayList类是一个线程安全的List接口的实现,在该类的内部进行元素的写操作时,底层的数组将被完整的复制,这对于读操作远远多于写操作的应用非常适合。在CopyOnWriteArrayList上进行操作时,读操作不需要加锁,而写操作类实现中对其进行了加锁。

二、具体实现

    CopyOnWriteArrayList底层的定义如下:

Java代码  收藏代码
  1. public class CopyOnWriteArrayList<E> 
  2.         implements List<E>, RandomAccess, Cloneable, java.io.Serializable { 
  3.  
  4.     private volatile transient E[] array; 
  5.  
  6.     private E[] array() { return array; } 
  7.  
  8.     // 该操作是加锁的,防止array在copy的时候被替换  
  9.     private synchronized void copyIn(E[] toCopyIn, int first, int n) { 
  10.         array  = (E[]) new Object[n]; 
  11.         System.arraycopy(toCopyIn, first, array, 0, n); 
  12.     } 
  13.  
  14.   ... 

    读写操作:

Java代码  收藏代码
  1. public class CopyOnWriteArrayList<E> 
  2.         implements List<E>, RandomAccess, Cloneable, java.io.Serializable { 
  3.  
  4.     public E get(int index) { 
  5.      // 由于包括rangeCheck和index两个操作,并不是直接在array上执行 
  6.    // 而是使用本地变量elementData引用array数组,防止两个操作之间 
  7.    // array被替换 
  8.      E[] elementData = array(); 
  9.         rangeCheck(index, elementData.length); 
  10.         return elementData[index]; 
  11.     } 
  12.      
  13.     public synchronized E set(int index, E element) { // 是同步的 
  14.         int len = array.length; 
  15.         rangeCheck(index, len); 
  16.         E oldValue = array[index]; 
  17.  
  18.         // 判断该写的元素与原数据是否相同 
  19.         boolean same = (oldValue == element || 
  20.         (element != null && element.equals(oldValue))); 
  21.          
  22.         if (!same) { 
  23.             // [1] 创建一个新数组,将原array的值拷贝至新数组 
  24.         E[] newArray = (E[]) new Object[len]; 
  25.             System.arraycopy(array, 0, newArray, 0, len); 
  26.             // [2] set的元素 
  27.         newArray[index] = element; 
  28.             // [3] 替换底层array数组 
  29.         array = newArray; 
  30.         } 
  31.         return oldValue; 
  32.     } 
  33.  
  34.   ... 

    add和remove也采用相同的技术:

Java代码  收藏代码
  1. public class CopyOnWriteArrayList<E> 
  2.         implements List<E>, RandomAccess, Cloneable, java.io.Serializable { 
  3.  
  4.     public synchronized boolean add(E element) { 
  5.         // [1] new and copy 
  6.         int len = array.length; 
  7.         E[] newArray = (E[]) new Object[len+1]; 
  8.         System.arraycopy(array, 0, newArray, 0, len); 
  9.         // [2] add element 
  10.         newArray[len] = element; 
  11.         // [3] change base array 
  12.         array = newArray; 
  13.         return true
  14.     } 
  15.  
  16.     public synchronized E remove(int index) { 
  17.         int len = array.length; 
  18.         rangeCheck(index, len); 
  19.         E oldValue = array[index]; 
  20.         // new一个新的数组 
  21.       E[] newArray = (E[]) new Object[len-1]; 
  22.         // copy index之前的元素 
  23.       System.arraycopy(array, 0, newArray, 0, index); 
  24.         // copy余下的元素 
  25.       int numMoved = len - index - 1
  26.         if (numMoved > 0
  27.             System.arraycopy(array, index+1, newArray, index, numMoved); 
  28.         // 替换array引用     
  29.      array = newArray; 
  30.         return oldValue; 
  31.     } 
  32.  
  33.   ... 

   特别注意:在CopyOnWriteArrayList上获得的Iterator是不能进行set和remove操作的,否则会抛出异常。

一、简介

    BlockingQueue接口定义了一种阻塞的FIFO queue,每一个BlockingQueue都有一个容量,让容量满时往BlockingQueue中添加数据时会造成阻塞,当容量为空时取元素操作会阻塞。ArrayBlockingQueue是对BlockingQueue的一个数组实现,它使用一把全局的锁并行对queue的读写操作,同时使用两个Condition阻塞容量为空时的取操作和容量满时的写操作。

二、具体实现

    ArrayBlockingQueue底层定义如下:

Java代码  收藏代码
  1. public class ArrayBlockingQueue<E> extends AbstractQueue<E> 
  2.         implements BlockingQueue<E>, java.io.Serializable { 
  3.  
  4.     // 使用循环数组来实现queue,初始时takeIndex和putIndex均为0 
  5.     private final E[] items; 
  6.     private transient int takeIndex; 
  7.     private transient int putIndex; 
  8.     private int count; 
  9.  
  10.     // 用于并发的锁和条件 
  11.    private final ReentrantLock lock; 
  12.     private final Condition notEmpty; 
  13.     private final Condition notFull; 
  14.  
  15.     /**
  16.      * 循环数组
  17.      * Circularly increment i.
  18.      */ 
  19.     final int inc(int i) { 
  20.         return (++i == items.length)? 0 : i; 
  21.     } 
  22.  
  23.     public ArrayBlockingQueue(int capacity, boolean fair) { 
  24.         if (capacity <= 0
  25.             throw new IllegalArgumentException(); 
  26.         this.items = (E[]) new Object[capacity]; 
  27.         // 分配锁及该锁上的condition 
  28.         lock = new ReentrantLock(fair); 
  29.         notEmpty = lock.newCondition(); 
  30.         notFull =  lock.newCondition(); 
  31.     } 
  32.  
  33.   ... 

   ArrayBlockingQueue的取操作:

Java代码  收藏代码
  1. public class ArrayBlockingQueue<E> extends AbstractQueue<E> 
  2.         implements BlockingQueue<E>, java.io.Serializable { 
  3.  
  4.     private E extract() { 
  5.         final E[] items = this.items; 
  6.         E x = items[takeIndex]; 
  7.         items[takeIndex] = null
  8.         takeIndex = inc(takeIndex); 
  9.         --count; 
  10.        // 激发notFull条件 
  11.         notFull.signal(); 
  12.         return x; 
  13.     } 
  14.  
  15.      /**
  16.         * condition的await的语义如下:
  17.      * 与condition相关的锁以原子方式释放,并禁用该线程
  18.      * 方法返回时,线程必须获得与该condition相关的锁
  19.      */ 
  20.     public E take() throws InterruptedException { 
  21.         final ReentrantLock lock = this.lock; 
  22.         lock.lockInterruptibly(); 
  23.         try
  24.             try
  25.                   // 等待notEmpty的条件 
  26.                 while (count == 0
  27.                     notEmpty.await(); 
  28.             } catch (InterruptedException ie) { 
  29.                 notEmpty.signal(); // propagate to non-interrupted thread 
  30.                 throw ie; 
  31.             } 
  32.             E x = extract(); 
  33.             return x; 
  34.         } finally
  35.             lock.unlock(); 
  36.         } 
  37.     } 
  38.  
  39.   ... 

   ArrayBlockingQueue的写操作:

Java代码  收藏代码
  1. public class ArrayBlockingQueue<E> extends AbstractQueue<E> 
  2.         implements BlockingQueue<E>, java.io.Serializable { 
  3.  
  4.     private void insert(E x) { 
  5.         items[putIndex] = x; 
  6.         putIndex = inc(putIndex); 
  7.         ++count; 
  8.         notEmpty.signal(); 
  9.     } 
  10.  
  11.     public void put(E o) throws InterruptedException { 
  12.         if (o == null) throw new NullPointerException(); 
  13.         final E[] items = this.items; 
  14.         final ReentrantLock lock = this.lock; 
  15.         lock.lockInterruptibly(); 
  16.         try
  17.             try
  18.                   // 等待notFull条件 
  19.            while (count == items.length) 
  20.                     notFull.await(); 
  21.             } catch (InterruptedException ie) { 
  22.                 notFull.signal(); // propagate to non-interrupted thread 
  23.                 throw ie; 
  24.             } 
  25.             insert(o); 
  26.         } finally
  27.             lock.unlock(); 
  28.         } 
  29.     } 
  30.  
  31.   ... 

    注意:ArrayBlockingQueue在读写操作上都需要锁住整个容器,因此吞吐量与一般的实现是相似的,适合于实现“生产者消费者”模式。

一、简介

    LinkedBlockingQueue是BlockingQueue的一种使用Link List的实现,它对头和尾(取和添加操作)采用两把不同的锁,相对于ArrayBlockingQueue提高了吞吐量。它也是一种阻塞型的容器,适合于实现“消费者生产者”模式。

二、具体实现

    LinkedBlockingQueue底层的定义如下:

Java代码  收藏代码
  1. public class LinkedBlockingQueue<E> extends AbstractQueue<E> 
  2.         implements BlockingQueue<E>, java.io.Serializable { 
  3.  
  4.     static class Node<E> { 
  5.         /** The item, volatile to ensure barrier separating write and read */ 
  6.         volatile E item; 
  7.         Node<E> next; 
  8.         Node(E x) { item = x; } 
  9.     } 
  10.  
  11.     // 支持原子操作 
  12.     private final AtomicInteger count = new AtomicInteger(0); 
  13.  
  14.     // 链表的头和尾 
  15.     private transient Node<E> head; 
  16.     private transient Node<E> last; 
  17.  
  18.     // 针对取和添加操作的两把锁及其上的条件 
  19.    private final ReentrantLock takeLock = new ReentrantLock(); 
  20.     private final Condition notEmpty = takeLock.newCondition(); 
  21.     private final ReentrantLock putLock = new ReentrantLock(); 
  22.     private final Condition notFull = putLock.newCondition(); 
  23.  
  24.    ... 

    LinkedBlockingQueue的添加操作:

Java代码  收藏代码
  1. public class LinkedBlockingQueue<E> extends AbstractQueue<E> 
  2.         implements BlockingQueue<E>, java.io.Serializable { 
  3.  
  4.     private void insert(E x) { 
  5.         last = last.next = new Node<E>(x); 
  6.     } 
  7.  
  8.     /**
  9.      * signal方法在被调用时,当前线程必须拥有该condition相关的锁!
  10.      * Signal a waiting take. Called only from put/offer (which do not
  11.      * otherwise ordinarily lock takeLock.)
  12.      */ 
  13.     private void signalNotEmpty() { 
  14.         final ReentrantLock takeLock = this.takeLock; 
  15.         takeLock.lock(); 
  16.         try
  17.             notEmpty.signal(); 
  18.         } finally
  19.             takeLock.unlock(); 
  20.         } 
  21.     } 
  22.  
  23.     public void put(E o) throws InterruptedException { 
  24.         if (o == null) throw new NullPointerException(); 
  25.         int c = -1
  26.         final ReentrantLock putLock = this.putLock; 
  27.         final AtomicInteger count = this.count; 
  28.         // 使用putLock 
  29.         putLock.lockInterruptibly(); 
  30.         try
  31.             try
  32.                   // 当容量已满时,等待notFull条件 
  33.             while (count.get() == capacity) 
  34.                     notFull.await(); 
  35.             } catch (InterruptedException ie) { 
  36.                 notFull.signal(); // propagate to a non-interrupted thread 
  37.                 throw ie; 
  38.             } 
  39.             insert(o); 
  40.             // 取出当前值,并将原数据增加1 
  41.             c = count.getAndIncrement(); 
  42.             // 容量不满,再次激活notFull上等待的put线程 
  43.         if (c + 1 < capacity) 
  44.                 notFull.signal(); 
  45.         } finally
  46.             putLock.unlock(); 
  47.         } 
  48.         // 必须先释放putLock再在notEmpty上signal,否则会造成死锁 
  49.      if (c == 0
  50.             signalNotEmpty(); 
  51.     } 
  52.  
  53.   ... 

    LinkedBlockingQueue的取操作:

Java代码  收藏代码
  1. public class LinkedBlockingQueue<E> extends AbstractQueue<E> 
  2.         implements BlockingQueue<E>, java.io.Serializable { 
  3.  
  4.     private E extract() { 
  5.         Node<E> first = head.next; 
  6.         head = first; 
  7.         E x = first.item; 
  8.         first.item = null
  9.         return x; 
  10.     } 
  11.  
  12.     private void signalNotFull() { 
  13.         final ReentrantLock putLock = this.putLock; 
  14.         putLock.lock(); 
  15.         try
  16.             notFull.signal(); 
  17.         } finally
  18.             putLock.unlock(); 
  19.         } 
  20.     } 
  21.  
  22.     public E take() throws InterruptedException { 
  23.         E x; 
  24.         int c = -1
  25.         final AtomicInteger count = this.count; 
  26.         final ReentrantLock takeLock = this.takeLock; 
  27.         // 使用takeLock 
  28.         takeLock.lockInterruptibly(); 
  29.         try
  30.             try
  31.                   // 若容量为空,等待notEmpty 
  32.                 while (count.get() == 0
  33.                     notEmpty.await(); 
  34.             } catch (InterruptedException ie) { 
  35.                 notEmpty.signal(); // propagate to a non-interrupted thread 
  36.                 throw ie; 
  37.             } 
  38.  
  39.             x = extract(); 
  40.             c = count.getAndDecrement(); 
  41.             // 再次激活notEmpty 
  42.             if (c > 1
  43.                 notEmpty.signal(); 
  44.         } finally
  45.             takeLock.unlock(); 
  46.         } 
  47.         // take执行之前容量已满,则激活notFull 
  48.         if (c == capacity) 
  49.             signalNotFull(); 
  50.         return x; 
  51.     } 
  52.  
  53.   ... 

一、简介

    ConcurrentHashMap是Map的一种并发实现,在该类中元素的read操作都是无锁了,而write操作需要被同步。这非常适合于读操作远大于写操作的情况。在实现过程中,ConcurrentHashMap将所有元素分成了若干个segment,每个segment是独立的,在一个segment上加锁并不影响其他segment的操作。segment本身是一个hashtable,对于一个加入ConcurrentHashMap的<key, value>对,key的hash值中的高位被用来索引segment,而低位用于segment中的索引。

二、segment实现

    segment是ConcurrentHashMap存储元素的基本段,它本身是一个hashtable的实现,read操作时无锁的,write需要同步,定义如下:

Java代码  收藏代码
  1. public class ConcurrentHashMap<K, V> extends AbstractMap<K, V> 
  2.         implements ConcurrentMap<K, V>, Serializable { 
  3.  
  4.  
  5.     /**
  6.      *  key, hash, next都是不可改的
  7.    *  value值可被重写
  8.    */ 
  9.     static final class HashEntry<K,V> { 
  10.         final K key; 
  11.         final int hash; 
  12.         volatile V value; 
  13.         final HashEntry<K,V> next; 
  14.  
  15.         ... 
  16.     } 
  17.  
  18.     static final class Segment<K,V> extends ReentrantLock implements Serializable { 
  19.  
  20.         transient volatile int count; 
  21.         transient volatile HashEntry[] table; 
  22.         // 当segment中元素个数达到threshold时,需要rehash 
  23.         transient int threshold; 
  24.     } 
  25.  
  26.   ... 
  27. }  

    segment的read操作:

Java代码  收藏代码
  1. static final class Segment<K,V> extends ReentrantLock implements Serializable { 
  2.  
  3.       HashEntry<K,V> getFirst(int hash) { 
  4.           HashEntry[] tab = table; 
  5.           return (HashEntry<K,V>) tab[hash & (tab.length - 1)]; 
  6.       } 
  7.      
  8.      V get(Object key, int hash) { // 该操作是无锁的 
  9.           if (count != 0) { // read-volatile 
  10.               HashEntry<K,V> e = getFirst(hash); 
  11.               while (e != null) { 
  12.                   if (e.hash == hash && key.equals(e.key)) { 
  13.                       V v = e.value; 
  14.                       if (v != null
  15.                           return v; 
  16.                       return readValueUnderLock(e); // recheck 
  17.                   } 
  18.                   e = e.next; 
  19.               } 
  20.           } 
  21.           return null
  22.       } 
  23.  
  24. ... 

    由于HashEntry当中的key和next都是final的,所以segment之上的操作不可能影响HashEntry列表之间相对的顺序,而value是可变的,当第一次读值失败时,尝试加锁读。

    segment的replace操作:

Java代码  收藏代码
  1. static final class Segment<K,V> extends ReentrantLock implements Serializable { 
  2.  
  3.        /**
  4.           * replace操作是就地替换,HashEntry的value是非final的
  5.        */ 
  6.         boolean replace(K key, int hash, V oldValue, V newValue) { 
  7.             lock();  // replace操作是同步的 
  8.         try
  9.                 // 得到该hash值对应的entry列表 
  10.            HashEntry<K,V> e = getFirst(hash); 
  11.                 while (e != null && (e.hash != hash || !key.equals(e.key))) 
  12.                     e = e.next; 
  13.  
  14.                 boolean replaced = false
  15.                 if (e != null && oldValue.equals(e.value)) { // 替换 
  16.               replaced = true
  17.                      e.value = newValue; 
  18.                 } 
  19.                 return replaced; 
  20.             } finally
  21.                 unlock(); 
  22.             } 
  23.         } 
  24.  
  25.   ... 

    segmet的put操作:

Java代码  收藏代码
  1. static final class Segment<K,V> extends ReentrantLock implements Serializable { 
  2.  
  3.         V put(K key, int hash, V value, boolean onlyIfAbsent) { 
  4.             lock(); // put是同步的 
  5.         try
  6.                 int c = count; 
  7.                 if (c++ > threshold) // ensure capacity 
  8.                     rehash(); 
  9.                 HashEntry[] tab = table; 
  10.                 int index = hash & (tab.length - 1); 
  11.                 HashEntry<K,V> first = (HashEntry<K,V>) tab[index]; 
  12.                 HashEntry<K,V> e = first; 
  13.                 while (e != null && (e.hash != hash || !key.equals(e.key))) 
  14.                     e = e.next; 
  15.  
  16.                 V oldValue; 
  17.                 if (e != null) { // 已存在则更新 
  18.              oldValue = e.value; 
  19.                     if (!onlyIfAbsent) 
  20.                         e.value = value; 
  21.                 } 
  22.                 else { // 新添加则加入列表头部 
  23.               oldValue = null
  24.                     ++modCount; 
  25.                     // HashEntry的next是只读的,新加入的entry只能放在头部 
  26.              tab[index] = new HashEntry<K,V>(key, hash, first, value); 
  27.                     count = c; // write-volatile 
  28.                 } 
  29.                 return oldValue; 
  30.             } finally
  31.                 unlock(); 
  32.             } 
  33.         } 
  34.  
  35.   ... 

    segment的remove操作一种copy on write 的方法,保留被删元素之后的列表,copy被删元素之前的hashEntry:

Java代码  收藏代码
  1. static final class Segment<K,V> extends ReentrantLock implements Serializable { 
  2.  
  3.         V remove(Object key, int hash, Object value) { 
  4.             lock(); 
  5.             try
  6.                 int c = count - 1
  7.                 HashEntry[] tab = table; 
  8.                 int index = hash & (tab.length - 1); 
  9.                 HashEntry<K,V> first = (HashEntry<K,V>)tab[index]; 
  10.                 HashEntry<K,V> e = first; 
  11.                 while (e != null && (e.hash != hash || !key.equals(e.key))) 
  12.                     e = e.next; 
  13.  
  14.                 V oldValue = null
  15.                 if (e != null) { 
  16.                     V v = e.value; 
  17.                     if (value == null || value.equals(v)) { // copy on write 
  18.                         oldValue = v; 
  19.                         ++modCount; 
  20.                         // e之后的列表可以保留,只需要重新创建e之前的HashEntry即可 
  21.                  HashEntry<K,V> newFirst = e.next; 
  22.                         // copy on write e之前的HashEntry 
  23.                         for (HashEntry<K,V> p = first; p != e; p = p.next) 
  24.                             newFirst = new HashEntry<K,V>(p.key, p.hash,   
  25.                                                           newFirst, p.value); 
  26.                         tab[index] = newFirst; 
  27.                         count = c; // write-volatile 
  28.                     } 
  29.                 } 
  30.                 return oldValue; 
  31.             } finally
  32.                 unlock(); 
  33.             } 
  34.         } 
  35.  
  36.   ... 

    segment的rehash操作实现比较特别,为了保证rehash过程中copy的元素尽可能少,segment在rehash时Entry入口的个数是以2的倍数增长,这可以保证一个entry在rehash之后要么在原来的列表中,要么在下一个列表中:

Java代码  收藏代码
  1. static final class Segment<K,V> extends ReentrantLock implements Serializable { 
  2.  
  3.         void rehash() { 
  4.             // 局部变量引用table 
  5.             HashEntry[] oldTable = table;             
  6.             int oldCapacity = oldTable.length; 
  7.             if (oldCapacity >= MAXIMUM_CAPACITY) 
  8.                 return
  9.  
  10.             // 右移1位相当于乘以2 
  11.             HashEntry[] newTable = new HashEntry[oldCapacity << 1]; 
  12.             threshold = (int)(newTable.length * loadFactor); 
  13.             int sizeMask = newTable.length - 1
  14.             for (int i = 0; i < oldCapacity ; i++) { 
  15.                 // 第i个entry列表 
  16.            HashEntry<K,V> e = (HashEntry<K,V>)oldTable[i]; 
  17.  
  18.                 if (e != null) { 
  19.                     HashEntry<K,V> next = e.next; 
  20.                     // 在新table上的索引 
  21.                     int idx = e.hash & sizeMask; 
  22.  
  23.                     if (next == null
  24.                         newTable[idx] = e; 
  25.                     else
  26.                         // 寻找该entry列表末端,rehash之后idx相同的元素 
  27.                         // 这些元素不需要被copy 
  28.                         HashEntry<K,V> lastRun = e; 
  29.                         int lastIdx = idx; 
  30.                         for (HashEntry<K,V> last = next; 
  31.                              last != null
  32.                              last = last.next) { 
  33.                             int k = last.hash & sizeMask; 
  34.                             if (k != lastIdx) { 
  35.                                 lastIdx = k; 
  36.                                 lastRun = last; 
  37.                             } 
  38.                         } 
  39.                         // 将lastRun之后的整个列表挂到新位置上 
  40.                         newTable[lastIdx] = lastRun; 
  41.  
  42.                         // Clone all remaining nodes 
  43.                         for (HashEntry<K,V> p = e; p != lastRun; p = p.next) { 
  44.                             int k = p.hash & sizeMask; 
  45.                             HashEntry<K,V> n = (HashEntry<K,V>)newTable[k]; 
  46.                             newTable[k] = new HashEntry<K,V>(p.key, p.hash, 
  47.                                                              n, p.value); 
  48.                         } 
  49.                     } 
  50.                 } 
  51.             } 
  52.             table = newTable; 
  53.         } 
  54.  
  55.   ... 

三、ConcurrentHashMap方法实现

    ConcurrentHashMap在Segment的基础上,通过首先将<key, value>对hash到一个segment,再由segment实现对entry的管理。

    ConcurrentHashMap的get实现:

Java代码  收藏代码
  1. public class ConcurrentHashMap<K, V> extends AbstractMap<K, V> 
  2.         implements ConcurrentMap<K, V>, Serializable { 
  3.  
  4.     final Segment<K,V> segmentFor(int hash) { 
  5.         return (Segment<K,V>) segments[(hash >>> segmentShift) & segmentMask]; 
  6.     } 
  7.  
  8.     public V get(Object key) { 
  9.         int hash = hash(key); // throws NullPointerException if key null 
  10.         return segmentFor(hash).get(key, hash); 
  11.     } 
  12.  
  13.   ... 

    ConcurrentHashMap的put和get方法:

Java代码  收藏代码
  1. public class ConcurrentHashMap<K, V> extends AbstractMap<K, V> 
  2.         implements ConcurrentMap<K, V>, Serializable { 
  3.  
  4.     public V put(K key, V value) { 
  5.         if (value == null
  6.             throw new NullPointerException(); 
  7.         int hash = hash(key); 
  8.         return segmentFor(hash).put(key, hash, value, false); 
  9.     } 
  10.  
  11.     public V remove(Object key) { 
  12.         int hash = hash(key); 
  13.         return segmentFor(hash).remove(key, hash, null); 
  14.     } 
  15.  
  16.   ... 
   ConcurrentLinkedQueue充分使用了atomic包的实现打造了一个无锁得并发线程安全的队列。对比锁机制的实现,个人认为使用无锁机制的难点在于要充分考虑线程间的协调。简单的说就是多个线程对内部数据结构进行访问时,如果其中一个线程执行的中途因为一些原因出现故障,其他的线程能够检测并帮助完成剩下的操作。这就需要把对数据结构的操作过程精细的划分成多个状态或阶段,考虑每个阶段或状态多线程访问会出现的情况。上述的难点在此次分析的并发Queue的实现中有很好的说明。首先看看其部分源码:
Java代码  收藏代码
  1. public class ConcurrentLinkedQueue<E> extends AbstractQueue<E> 
  2.         implements Queue<E>, java.io.Serializable { 
  3.     private static final long serialVersionUID = 196745693267521676L; 
  4.  
  5.     private static class Node<E> { 
  6.         private volatile E item; 
  7.         private volatile Node<E> next; 
  8.  
  9.         private static final 
  10.             AtomicReferenceFieldUpdater<Node, Node> 
  11.             nextUpdater = 
  12.             AtomicReferenceFieldUpdater.newUpdater 
  13.             (Node.class, Node.class, "next"); 
  14.         private static final 
  15.             AtomicReferenceFieldUpdater<Node, Object> 
  16.             itemUpdater = 
  17.             AtomicReferenceFieldUpdater.newUpdater 
  18.             (Node.class, Object.class, "item"); 
  19.  
  20.         Node(E x) { item = x; } 
  21.  
  22.         Node(E x, Node<E> n) { item = x; next = n; } 
  23.  
  24.         E getItem() { 
  25.             return item; 
  26.         } 
  27.  
  28.         boolean casItem(E cmp, E val) { 
  29.             return itemUpdater.compareAndSet(this, cmp, val); 
  30.         } 
  31.  
  32.         void setItem(E val) { 
  33.             itemUpdater.set(this, val); 
  34.         } 
  35.  
  36.         Node<E> getNext() { 
  37.             return next; 
  38.         } 
  39.  
  40.         boolean casNext(Node<E> cmp, Node<E> val) { 
  41.             return nextUpdater.compareAndSet(this, cmp, val); 
  42.         } 
  43.  
  44.         void setNext(Node<E> val) { 
  45.             nextUpdater.set(this, val); 
  46.         } 
  47.  
  48.     } 
  49.  
  50.     private static final 
  51.         AtomicReferenceFieldUpdater<ConcurrentLinkedQueue, Node> 
  52.         tailUpdater = 
  53.         AtomicReferenceFieldUpdater.newUpdater 
  54.         (ConcurrentLinkedQueue.class, Node.class, "tail"); 
  55.     private static final 
  56.         AtomicReferenceFieldUpdater<ConcurrentLinkedQueue, Node> 
  57.         headUpdater = 
  58.         AtomicReferenceFieldUpdater.newUpdater 
  59.         (ConcurrentLinkedQueue.class,  Node.class, "head"); 
  60.  
  61.     private boolean casTail(Node<E> cmp, Node<E> val) { 
  62.         return tailUpdater.compareAndSet(this, cmp, val); 
  63.     } 
  64.  
  65.     private boolean casHead(Node<E> cmp, Node<E> val) { 
  66.         return headUpdater.compareAndSet(this, cmp, val); 
  67.     } 
  68.  
  69.     private transient volatile Node<E> head = new Node<E>(null, null); 
  70.  
  71.     private transient volatile Node<E> tail = head; 
  72.     ... 

    先看看其内部数据结构Node的实现。由于使用了原子字段更新器AtomicReferenceFieldUpdater<T,V>(其中T表示持有字段的类的类型,V表示字段的类型),所以其对应的需要更新的字段要使用volatile进行声明。其newUpdater(Class<U> tclass, Class<W> vclass, String fieldName)方法实例化一个指定字段的更新器,参数分别表示:持有需要更新字段的类,字段的类,要更新的字段的名称。Node的内部变量item,next分别有对应自己的字段更新器,并且包含了对其原子性操作的方法compareAndSet(T obj, V expect, V update),其中T是持有被设置字段的对象,后两者分别是期望值和新值。

    对于ConcurrentLinkedQueue自身也有两个volatile的线程共享变量:head,tail分别对应队列的头指针和尾指针。要保证这个队列的线程安全就是保证对这两个Node的引用的访问(更新,查看)的原子性和可见性,由于volatile本身能够保证可见性,所以就是对其修改的原子性要被保证。下面看看其对应的方法是如何完成的。
Java代码  收藏代码
  1. public boolean offer(E e) { 
  2.     if (e == null) throw new NullPointerException(); 
  3.     Node<E> n = new Node<E>(e, null); 
  4.     for (;;) { 
  5.         Node<E> t = tail; 
  6.         Node<E> s = t.getNext(); 
  7.         if (t == tail) { //------------------------------a 
  8.             if (s == null) { //---------------------------b 
  9.                 if (t.casNext(s, n)) { //-------------------c 
  10.                     casTail(t, n); //------------------------d 
  11.                     return true
  12.                 } 
  13.             } else
  14.                 casTail(t, s); //----------------------------e 
  15.             } 
  16.         } 
  17.     } 

offer()方法都很熟悉了,就是入队的操作。涉及到改变尾指针的操作,所以要看这个方法实现是否保证了原子性。CAS操作配合循环是原子性操作的保证,这里也不例外。此方法的循环内首先获得尾指针和其next指向的对象,由于tail和Node的next均是volatile的,所以保证了获得的分别都是最新的值。
    代码a :t==tail是最上层的协调,如果其他线程改变了tail的引用,则说明现在获得不是最新的尾指针需要重新循环获得最新的值。
    代码b :s==null的判断。静止状态下tail的next一定是指向null的,但是多线程下的另一个状态就是中间态:tail的指向没有改变,但是其next已经指向新的结点,即完成tail引用改变前的状态,这时候s!=null。这里就是协调的典型应用,直接进入 代码e 去协调参与中间态的线程去完成最后的更新,然后重新循环获得新的tail开始自己的新一次的入队尝试。另外值得注意的是a,b之间,其他的线程可能会改变tail的指向,使得协调的操作失败。从这个步骤可以看到无锁实现的复杂性。
    代码c :t.casNext(s, n)是入队的第一步,因为入队需要两步:更新Node的next,改变tail的指向。代码c之前可能发生tail引用指向的改变或者进入更新的中间态,这两种情况均会使得t指向的元素的next属性被原子的改变,不再指向null。这时代码c操作失败,重新进入循环。
    代码d :这是完成更新的最后一步了,就是更新tail的指向,最有意思的协调在这儿又有了体现。从代码看casTail(t, n)不管是否成功都会接着返回true标志着更新的成功。首先如果成功则表明本线程完成了两步的更新,返回true是理所当然的;如果 casTail(t, n)不成功呢?要清楚的是完成代码c则代表着更新进入了中间态,代码d不成功则是tail的指向被其他线程改变。意味着对于其他的线程而言:它们得到的是中间态的更新,s!=null,进入 代码e 帮助本线程执行最后一步并且先于本线程成功。这样本线程虽然代码d失败了,但是是由于别的线程的协助先完成了,所以返回true也就理所当然了。
   
    通过分析这个入队的操作,可以清晰的看到无锁实现的每个步骤和状态下多线程之间的协调和工作。理解了入队的整个过程,出队的操作poll()的实现也就变得简单了。基本上是大同小异的,无非就是同时牵涉到了head和tail的状态,在改变head的同时照顾到tail的协调,在此不多赘述。下面介绍一下其无锁下的查看访问,其内部不单单是查看更包含了线程间的协调,这是无锁实现的一个特点。不管是contains(),size()还是isEmpty(),只要获得了head后面第一个最新的Node就可以很轻松的实现,毕竟Node的getNext()和getItem()返回的都是对应的最新值。所以先看看这些方法内部的first()如何获得最新的第一个Node:
Java代码  收藏代码
  1. Node<E> first() { 
  2.     for (;;) { 
  3.         Node<E> h = head; 
  4.         Node<E> t = tail; 
  5.         Node<E> first = h.getNext(); 
  6.         if (h == head) { //---------------------------------------a 
  7.             if (h == t) { //-----------------------------------------b 
  8.                 if (first == null) //----------------------------------c 
  9.                     return null
  10.                 else 
  11.                     casTail(t, first); //--------------------------------d 
  12.             } else
  13.                 if (first.getItem() != null) //------------------------e 
  14.                     return first; 
  15.                 else // remove deleted node and continue 
  16.                     casHead(h, first); //------------------------------f 
  17.             } 
  18.         } 
  19.     } 

此方法在尝试获得最新的第一个非head结点的时候,在不同的阶段同样在协调着head和tail的更新任务,让人感觉无锁的世界没有纯粹的工作,呵呵。
    代码a :还是最上层的协调,head指向没改变的情况下才继续下面的操作。这时侯head只可能是静止的,因为poll()出队操作的步骤是反着的:首先更新head的指向进入中间态,然后更新原head的next的item为null。
    代码b :之所以h==t的情况独立于其他的情况(在出队poll()方法中同样),主要是因为first!=null时可能对应着某一个更新的中间态,而产生中间态的的必要条件就是代码b成立。如果h==t则表示当前线程获得的首尾指针指向同一个结点,当然代码b执行之后可能其他线程会进行head或者tail的更新。
    代码c :first==null表明tail并没有进入更新的中间态而是处于静止状态,并且由于tail指向的是head的指向,所以返回null是唯一的选择。但是这美好的一切都是建立在代码b和代码c之间没有其他的线程更新tail。一旦有其他的线程执行了入队的操作并至少进入中间态的话,h==t和first==null都遗憾的成立,这就造成了取得幻象值,而实际上h.getNext()已经不再为null。 个人认为代码c改成if((first = h.getNext()) == null)更能提高命中率
    代码d :只要first!=null(不管是本人修改的代码还是源码)本线程则去尝试协调其他的线程先完成tail的更新,等待循环再次获取最新的head和tail。
    代码e :此处first一定不为null,tail更新与否不影响first的item的获取,但是head的更新会有影响。如果head正在被另一个线程更新并进入中间态,既是poll()内的else if (casHead(h, first)) 成功,但是并没有执行first.setItem(null)之前。此时代码e是满足的,返回的也是当前的first的,但是随后head全部更新成功则first的item为null。所以此处返回的first的item并不一定是item!=null的结点,在使用此方法获得的结点的item时一定要再次的进行判断,这点在contains(...)等方法内都有体现。
    代码f :如果first的item==null,则更新head的指向。直观上看似乎多余,因为出队的操作是先更新head的指向再更新item为null的。但是另一个方法remove(...)则仅仅更新item的值而不改变head的指向,所以针对这样的多线程调用,代码f变得非常的必需了。

    这样通过这两个方法的分析可以推及对ConcurrentLinkedQueue共享变量的其他操作的实现,这样的无锁的实现印象最深的就是要考虑线程间的协调。不像锁机制的实现虽然牺牲了一定的性能,但是至少操作这些非线程安全的共享变量时不用过多的考虑其他线程的操作。至此才算体会到无锁实现的复杂性,这或许就是有得必有失吧,呵呵。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值