Java中CopyOnWriteArrayList有什么用

发布时间:2021-06-15 14:55:17 作者:Leah
来源:亿速云 阅读:166

本篇文章给大家分享的是有关Java中CopyOnWriteArrayList有什么用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

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

以上就是Java中CopyOnWriteArrayList有什么用,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

推荐阅读:
  1. Java并发-CopyOnWriteArrayList
  2. java中generic有什么用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java copyonwritearraylist

上一篇:Java之Intern详解

下一篇:mac中怎么安装mysql

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》