Java中CopyOnWriteArrayList有什么用

本篇文章給大家分享的是有關(guān)Java中CopyOnWriteArrayList有什么用,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

目前成都創(chuàng)新互聯(lián)公司已為上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、安州網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶(hù)導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶(hù)和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

ArrayList和HashMap是我們經(jīng)常使用的集合,它們不是線程安全的。我們一般都知道HashMap的線程安全版本為ConcurrentHashMap,那么ArrayList有沒(méi)有類(lèi)似的線程安全的版本呢?還真有,它就是CopyOnWriteArrayList。

CopyOnWrite這個(gè)短語(yǔ),還有一個(gè)專(zhuān)門(mén)的稱(chēng)謂COW. COW不僅僅是java實(shí)現(xiàn)集合框架時(shí)專(zhuān)用的機(jī)制,它在計(jì)算機(jī)中被廣泛使用。

首先看一下什么是CopyOnWriteArrayList,它的類(lèi)前面的javadoc注釋很長(zhǎng),我們只截取最前面的一小段。如下。它的介紹中說(shuō)到,CopyOnWriteArrayList是ArrayList的一個(gè)線程安全的變種,在CopyOnWriteArrayList中,所有改變操作(add,set等)都是通過(guò)給array做一個(gè)新的拷貝來(lái)實(shí)現(xiàn)的。通常來(lái)看,這花費(fèi)的代價(jià)太大了,但是,當(dāng)讀取list的線程數(shù)量遠(yuǎn)遠(yuǎn)多于寫(xiě)list的線程數(shù)量時(shí),這種方法依然比別的實(shí)現(xiàn)方式更高效。

/**
 * 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個(gè),一個(gè)是基本數(shù)據(jù)結(jié)構(gòu)array,用于保存數(shù)據(jù),一個(gè)是可重入鎖,它用于寫(xiě)操作的同步。

  /** 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方法沒(méi)有什么特殊之處,不加鎖,直接讀取即可。

  /**
   * {@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拷貝到一個(gè)新的數(shù)組中,并把待添加的元素加入到新數(shù)組,最后,再把新數(shù)組賦值給原數(shù)組。這里可以看到,add操作并不是直接在原數(shù)組上操作,而是把整個(gè)數(shù)據(jù)進(jìn)行了拷貝,才操作的,最后把新數(shù)組賦值回去。

  /**
   * 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;
  }

這里,思考一個(gè)問(wèn)題。線程1正在遍歷list,此時(shí),線程2對(duì)線程進(jìn)行了寫(xiě)入,那么,線程1可以遍歷到線程2寫(xiě)入的數(shù)據(jù)嗎?

首先明確一點(diǎn),這個(gè)場(chǎng)景不會(huì)拋出任何異常,程序會(huì)安靜的執(zhí)行完成。是否能到讀到線程2寫(xiě)入的數(shù)據(jù),取決于遍歷方式和線程2的寫(xiě)入時(shí)機(jī)及位置。

首先看遍歷方式,我們2中方式遍歷list,foreach和get(i)的方式。foreach的底層實(shí)現(xiàn)是迭代器,所以迭代器就不單獨(dú)作為一種遍歷方式了。首先看一下通過(guò)for循環(huán)get(i)的方式。這種遍歷方式下,能否讀取到線程2寫(xiě)入的數(shù)據(jù),取決了線程2的寫(xiě)入時(shí)機(jī)和位置。如果線程1已經(jīng)遍歷到第5個(gè)元素了,那么如果線程2在第5個(gè)后面進(jìn)行寫(xiě)入,那么線程1就可以讀取到線程2的寫(xiě)入。

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");
    //啟動(dòng)線程1,遍歷數(shù)據(jù)
    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寫(xiě)入數(shù)據(jù),即,在遍歷位置之后寫(xiě)入數(shù)據(jù)
    list.add(4,"n");
  }
}

上述程序的運(yùn)行結(jié)果如下,是可以遍歷到n的。

a
b
c
d
n
e
f
g
h

如果線程2在第5個(gè)位置前面寫(xiě)入,那么線程1就讀取不到線程2的寫(xiě)入。同時(shí),還會(huì)帶來(lái)一個(gè)副作用,就是某個(gè)元素會(huì)被讀取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");
    //啟動(dòng)線程1,遍歷數(shù)據(jù)
    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寫(xiě)入數(shù)據(jù),即,在遍歷位置之后寫(xiě)入數(shù)據(jù)
    list.add(1,"n");
  }
}

上述代碼的運(yùn)行結(jié)果如下,其中,b被遍歷了2次。

a
b
b
c
d
e
f
g
h

那么,采用foreach方式遍歷呢?答案是無(wú)論線程2寫(xiě)入時(shí)機(jī)如何,線程2都無(wú)法讀取到線程2的寫(xiě)入。原因在于CopyOnWriteArrayList在創(chuàng)建迭代器時(shí),取了當(dāng)前時(shí)刻數(shù)組的快照。并且,add操作只會(huì)影響原數(shù)組,影響不到迭代器中的快照。

  public Iterator<E> iterator() {
    return new COWIterator<E>(getArray(), 0);
  }
  private COWIterator(Object[] elements, int initialCursor) {
      cursor = initialCursor;
      snapshot = elements;
  }

以上就是Java中CopyOnWriteArrayList有什么用,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

標(biāo)題名稱(chēng):Java中CopyOnWriteArrayList有什么用
文章地址:http://muchs.cn/article8/pdpsip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、響應(yīng)式網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)公司、外貿(mào)建站、自適應(yīng)網(wǎng)站、定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)