JAVA并发学习-并发容器CopyOnWriteArrayList

java8 CopyOnWriteArrayList
使用CopyOnWriteArrayList在迭代器迭代期间不需要向容器加锁或复制,就能得到很好的并发性能
内部结构:采用数组存储

 private transient volatile Object[] array;

构造函数:

    /**
 * Creates an empty list.
 */
public CopyOnWriteArrayList() {
    setArray(new Object[0]);//新建一个容量为0的数组赋值给array
}

/**
 * Creates a list containing the elements of the specified
 * collection, in the order they are returned by the collection's
 * iterator.
 *
 * @param c the collection of initially held elements
 * @throws NullPointerException if the specified collection is null
 */
public CopyOnWriteArrayList(Collection c) {
    Object[] elements;
    if (c.getClass() == CopyOnWriteArrayList.class)
        elements = ((CopyOnWriteArrayList)c).getArray();
    else {
        elements = c.toArray();
        // c.toArray might (incorrectly) not return Object[] (see 6260652)
        if (elements.getClass() != Object[].class)
            elements = Arrays.copyOf(elements, elements.length, Object[].class);
    }
    setArray(elements);
}

/**
 * Creates a list holding a copy of the given array.
 *
 * @param toCopyIn the array (a copy of this array is used as the
 *        internal array)
 * @throws NullPointerException if the specified array is null
 */
public CopyOnWriteArrayList(E[] toCopyIn) {
    setArray(Arrays.copyOf(toCopyIn, toCopyIn.length, Object[].class));
}

关键方法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();
    }
}

通过add()方法源码可以看到:当要修改时先加上可重入锁ReentrantLock,使得只有一个线程可以进入add方法锁住的代码块,再复制底层数组得到副本,对副本进行修改,再将修改后的副本替换原来的array

get方法

 private E get(Object[] a, int index) {
    return (E) a[index];
}

/**
 * {@inheritDoc}
 *
 * @throws IndexOutOfBoundsException {@inheritDoc}
 */
public E get(int index) {
    return get(getArray(), index);
}

读不加锁,“写入时复制“容器的迭代器会保留对底层基础数组的一个引用,写操作不是操作底层基本数组。所有有多个线程进行迭代的过程中不会彼此干扰也不会与修改容器的线程干扰,不会抛出ConCurrentModificationException异常

什么情况下加锁、什么情况下不加锁
写加锁,读不加所

适用场景
适用于读远多于写的场合,因为写时需要复制底层数组,需要一定的开销,特别是数组规模特别庞大的时候。

你可能感兴趣的:(JAVA并发学习-并发容器CopyOnWriteArrayList)