深入Java集合學習系列:CopyOnWriteArrayList詳解

http://my.oschina.net/jielucky/blog/167198

http://my.oschina.net/summerpxy/blog/405728

CopyOnWriteArrayList是ArrayList 的一個線程安全的變體,其中所有可變操作(add、set等等)都是通過對底層數組進行一次新的複製來實現的。

 這一般需要很大的開銷,但是當遍歷操作的數量大大超過可變操作的數量時,這種方法可能比其他替代方法更 有效。在不能或不想進行同步遍歷,但又需要從併發線程中排除衝突時,它也很有用。“快照”風格的迭代器方法在創建迭代器時使用了對數組狀態的引用。此數組在迭代器的生存期內不會更改,因此不可能發生衝突,並且迭代器保證不會拋出ConcurrentModificationException。創建迭代器以後,迭代器就不會反映列表的添加、移除或者更改。在迭代器上進行的元素更改操作(remove、set和add)不受支持。這些方法將拋出UnsupportedOperationException。允許使用所有元素,包括null。

內存一致性效果:當存在其他併發 collection 時,將對象放入CopyOnWriteArrayList之前的線程中的操作 happen-before 隨後通過另一線程從CopyOnWriteArrayList中訪問或移除該元素的操作。 

這種情況一般在多線程操作時,一個線程對list進行修改。一個線程對list進行fore時會出現java.util.ConcurrentModificationException錯誤。

下面來看一個列子:兩個線程一個線程fore一個線程修改list的值。
?

package

com.lucky.concurrent.list;

import

java.util.ArrayList;

import

java.util.List;

import

java.util.concurrent.ExecutorService;

import

java.util.concurrent.Executors;

public

class
CopyOnWriteArrayListDemo {

/**

 *

讀線程

 *

@author wangjie

 *

 */

private

static
class
ReadTask implements

Runnable {

    List<String>

list;

    public

ReadTask(List list) {

        this.list

= list;

    }



    public

void
run() {

        for

(String str : list) {

            System.out.println(str);

        }

    }

}

/**

 *

寫線程

 *

@author wangjie

 *

 */

private

static
class
WriteTask implements

Runnable {

    List<String>

list;

    int

index;

    public

WriteTask(List list, int

index) {

        this.list

= list;

        this.index

= index;

    }



    public

void
run() {

        list.remove(index);

        list.add(index,

“write_”

  • index);

    }
    

    }

    public

void
run() {

    final

int
NUM = 10;

    List<String>

list = new

ArrayList();

    for

(int

i = 0;
i < NUM; i++) {

        list.add("main_"
  • i);

    }
    
    ExecutorService
    

    executorService = Executors.newFixedThreadPool(NUM);

    for
    

(int

i = 0;
i < NUM; i++) {

        executorService.execute(new

ReadTask(list));

        executorService.execute(new

WriteTask(list, i));

    }

    executorService.shutdown();

}



public

static
void
main(String[] args) {

    new

CopyOnWriteArrayListDemo().run();

}

}
運行結果:

從結果中可以看出來。在多線程情況下報錯。其原因就是多線程操作結果:那這個種方案不行我們就換個方案。用jdk自帶的類CopyOnWriteArrayList來做容器。這個類和ArrayList最大的區別就是add(E) 的時候。容器會自動copy一份出來然後再尾部add(E)。看源碼:
?

/**

*

Appends the specified element to the end of this list.

*

*

@param e element to be appended to this list

*

@return 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();

}

}

用到了Arrays.copyOf 方法。這樣導致每次操作的都不是同一個引用。也就不會出現java.util.ConcurrentModificationException錯誤。
換了種方案看代碼:
?

//
List list = new ArrayList();

    CopyOnWriteArrayList<String>

list = new

CopyOnWriteArrayList();
也就把容器list換成了 CopyOnWriteArrayList,其他的沒變。線程裏面的list不用改。因爲 CopyOnWriteArrayList實現的也是list 接口。看結果:

其結果沒報錯。
CopyOnWriteArrayList add(E) 和remove(int index)都是對新的數組進行修改和新增。所以在多線程操作時不會出現java.util.ConcurrentModificationException錯誤。
所以最後得出結論:CopyOnWriteArrayList適合使用在讀操作遠遠大於寫操作的場景裏,比如緩存。發生修改時候做copy,新老版本分離,保證讀的高性能,適用於以讀爲主的情況。

 CopyOnWriteArrayList類是高效的線程安全的類。線程安全是因爲該類對於所有的修改方法都使用了加鎖操作。高效是因爲對於迭代操作是對原有集合的引用,避免了同步的開銷。

?

/* The lock protecting all mutators /

transient final ReentrantLock lock = new ReentrantLock();



/** The array, accessed only via getArray/setArray. */

private volatile transient Object[] array;

這兩個成員變量是該類的核心,對集合所有的修改操作都需要使用lock加鎖,array則是整個集合的數據儲存部分,關鍵在於該array被聲明爲volatile,當一個線程對與線程中array副本的修改會立即同步到主內存中該變量中去。

?

public CopyOnWriteArrayList(Collection

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章