本文基於jdk1.8進行分析。
ArrayList和HashMap是我們經常使用的集合,它們不是線程安全的。我們一般都知道HashMap的線程安全版本爲ConcurrentHashMap,那麼ArrayList有沒有類似的線程安全的版本呢?還真有,它就是CopyOnWriteArrayList。
CopyOnWrite這個短語,還有一個專門的稱謂COW. COW不僅僅是java實現集合框架時專用的機制,它在計算機中被廣泛使用。
首先看一下什麼是CopyOnWriteArrayList,它的類前面的javadoc註釋很長,我們只截取最前面的一小段。如下。它的介紹中說到,CopyOnWriteArrayList是ArrayList的一個線程安全的變種,在CopyOnWriteArrayList中,所有改變操作(add,set等)都是通過給array做一個新的拷貝來實現的。通常來看,這花費的代價太大了,但是,當讀取list的線程數量遠遠多於寫list的線程數量時,這種方法依然比別的實現方式更高效。
/**
* A thread-safe variant of {@link java.util.ArrayList} in which all mutative
* operations ({@code add}, {@code set}, and so on) are implemented by
* making a fresh copy of the underlying array.
*
* <p>This is ordinarily too costly, but may be <em>more</em> efficient
* than alternatives when traversal operations vastly outnumber
* mutations, and is useful when you cannot or don't want to
* synchronize traversals, yet need to preclude interference among
* concurrent threads. The "snapshot" style iterator method uses a
* reference to the state of the array at the point that the iterator
* was created. This array never changes during the lifetime of the
* iterator, so interference is impossible and the iterator is
* guaranteed not to throw {@code ConcurrentModificationException}.
* The iterator will not reflect additions, removals, or changes to
* the list since the iterator was created. Element-changing
* operations on iterators themselves ({@code remove}, {@code set}, and
* {@code add}) are not supported. These methods throw
* {@code UnsupportedOperationException}.
*
下面看一下成員變量。只有2個,一個是基本數據結構array,用於保存數據,一個是可重入鎖,它用於寫操作的同步。
/** The lock protecting all mutators */
final transient ReentrantLock lock = new ReentrantLock();
/** The array, accessed only via getArray/setArray. */
private transient volatile Object[] array;
下面看一下主要方法。get方法如下。get方法沒有什麼特殊之處,不加鎖,直接讀取即可。
/**
* {@inheritDoc}
*
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E get(int index) {
return get(getArray(), index);
}
/**
* Gets the array. Non-private so as to also be accessible
* from CopyOnWriteArraySet class.
*/
final Object[] getArray() {
return array;
}
@SuppressWarnings("unchecked")
private E get(Object[] a, int index) {
return (E) a[index];
}
下面看一下add。add方法先加鎖,然後,把原array拷貝到一個新的數組中,並把待添加的元素加入到新數組,最後,再把新數組賦值給原數組。這裏可以看到,add操作並不是直接在原數組上操作,而是把整個數據進行了拷貝,才操作的,最後把新數組賦值回去。
/**
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return {@code true} (as specified by {@link Collection#add})
*/
public boolean add(E e) {
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] elements = getArray();
int len = elements.length;
Object[] newElements = Arrays.copyOf(elements, len + 1);
newElements[len] = e;
setArray(newElements);
return true;
} finally {
lock.unlock();
}
}
/**
* Sets the array.
*/
final void setArray(Object[] a) {
array = a;
}
這裏,思考一個問題。線程1正在遍歷list,此時,線程2對線程進行了寫入,那麼,線程1可以遍歷到線程2寫入的數據嗎?
首先明確一點,這個場景不會拋出任何異常,程序會安靜的執行完成。是否能到讀到線程2寫入的數據,取決於遍歷方式和線程2的寫入時機及位置。
首先看遍歷方式,我們2中方式遍歷list,foreach和get(i)的方式。foreach的底層實現是迭代器,所以迭代器就不單獨作爲一種遍歷方式了。首先看一下通過for循環get(i)的方式。這種遍歷方式下,能否讀取到線程2寫入的數據,取決了線程2的寫入時機和位置。如果線程1已經遍歷到第5個元素了,那麼如果線程2在第5個後面進行寫入,那麼線程1就可以讀取到線程2的寫入。
public class MyClass {
static List<String> list = new CopyOnWriteArrayList<>();
public static void main(String[] args){
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
list.add("f");
list.add("g");
list.add("h");
//啓動線程1,遍歷數據
new Thread(()->{
try{
for(int i = 0; i < list.size();i ++){
System.out.println(list.get(i));
Thread.sleep(1000);
}
}catch (Exception e){
e.printStackTrace();
}
}).start();
try{
//主線程作爲線程2,等待2s
Thread.sleep(2000);
}catch (Exception e){
e.printStackTrace();
}
//主線程作爲線程2,在位置4寫入數據,即,在遍歷位置之後寫入數據
list.add(4,"n");
}
}
上述程序的運行結果如下,是可以遍歷到n的。
a
b
c
d
n
e
f
g
h
如果線程2在第5個位置前面寫入,那麼線程1就讀取不到線程2的寫入。同時,還會帶來一個副作用,就是某個元素會被讀取2次。代碼如下:
public class MyClass {
static List<String> list = new CopyOnWriteArrayList<>();
public static void main(String[] args){
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
list.add("f");
list.add("g");
list.add("h");
//啓動線程1,遍歷數據
new Thread(()->{
try{
for(int i = 0; i < list.size();i ++){
System.out.println(list.get(i));
Thread.sleep(1000);
}
}catch (Exception e){
e.printStackTrace();
}
}).start();
try{
//主線程作爲線程2,等待2s
Thread.sleep(2000);
}catch (Exception e){
e.printStackTrace();
}
//主線程作爲線程2,在位置1寫入數據,即,在遍歷位置之後寫入數據
list.add(1,"n");
}
}
上述代碼的運行結果如下,其中,b被遍歷了2次。
a
b
b
c
d
e
f
g
h
那麼,採用foreach方式遍歷呢?答案是無論線程2寫入時機如何,線程2都無法讀取到線程2的寫入。原因在於CopyOnWriteArrayList在創建迭代器時,取了當前時刻數組的快照。並且,add操作只會影響原數組,影響不到迭代器中的快照。
public Iterator<E> iterator() {
return new COWIterator<E>(getArray(), 0);
}
private COWIterator(Object[] elements, int initialCursor) {
cursor = initialCursor;
snapshot = elements;
}
瞭解清楚了遍歷方式和寫入時機對是否能夠讀取到寫入的影響,我們在使用CopyOnWriteArrayList時就可以根據實際業務場景的需求,選擇合適的實現方式了。