高并发操作共享的CopyOnWriteArrayList集合时,在一个线程做读操作其它线程做删除新增操作导致集合的大小发生变化,但是读操作线程不会发生异常,是因为删除添加操作并不是在集合的原Object[]数组上面操作的,而是拷贝了一个新的数组在,先数组上面完成的操作。
高并发操作共性的集合时,多个线程同时做删除和新增操作,而集合的数据保持正确,是因为集合内部使用了同一个锁final transient ReentrantLock lock = new ReentrantLock();
1、集合get(int index)方法
如下源码所以获取集合中的值,是直接从Object[]数组中获取。
/** The array, accessed only via getArray/setArray. */ private transient volatile Object[] array; /** * Gets the array. Non-private so as to also be accessible * from CopyOnWriteArraySet class. */ final Object[] getArray() { return array; } /** * {@inheritDoc} * * @throws IndexOutOfBoundsException {@inheritDoc} */ public E get(int index) { return get(getArray(), index); }
2、集合add(E e)方法
如下源码所示,在集合添加时,并不是直接往Object[]数组中插入数据而是先加锁再先复制一个新的数组Object[] newElements,把旧数组的数据拷贝大新数组并把数据赋值给数组最后,然后将新数组复制给集合的Object数组。
/** * 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(); } }
3、集合add(int index, E element) 方法
如下源码所示,在集合特定位置添加时,并不是直接往Object[]数组中插入数据而是先加锁再先复制一个新的数组Object[] newElements,把旧数组0到index的数据拷贝到新数据然后在index处查新新数据,再把就数组剩余的数据拷贝到新数组的后边,然后将新数组复制给集合的Object数组。
/** * Inserts the specified element at the specified position in this * list. Shifts the element currently at that position (if any) and * any subsequent elements to the right (adds one to their indices). * * @throws IndexOutOfBoundsException {@inheritDoc} */ public void add(int index, E element) { final ReentrantLock lock = this.lock; lock.lock(); try { Object[] elements = getArray(); int len = elements.length; if (index > len || index < 0) throw new IndexOutOfBoundsException("Index: "+index+ ", Size: "+len); Object[] newElements; int numMoved = len - index; if (numMoved == 0) newElements = Arrays.copyOf(elements, len + 1); else { newElements = new Object[len + 1]; System.arraycopy(elements, 0, newElements, 0, index); System.arraycopy(elements, index, newElements, index + 1, numMoved); } newElements[index] = element; setArray(newElements); } finally { lock.unlock(); } }
4、集合E remove(int index)方法
如下源码所示,在移除集合数据时,并不是直接往Object[]数组中数据删除而是先加锁再先复制一个新的数组Object[] newElements,把就数组0到index的数据拷贝到新数据,再把旧数组index+1以后的数据拷贝到新数组的后边,然后将新数组复制给集合的Object数组。
/** * Removes the element at the specified position in this list. * Shifts any subsequent elements to the left (subtracts one from their * indices). Returns the element that was removed from the list. * * @throws IndexOutOfBoundsException {@inheritDoc} */ public E remove(int index) { final ReentrantLock lock = this.lock; lock.lock(); try { Object[] elements = getArray(); int len = elements.length; E oldValue = get(elements, index); int numMoved = len - index - 1; if (numMoved == 0) setArray(Arrays.copyOf(elements, len - 1)); else { Object[] newElements = new Object[len - 1]; System.arraycopy(elements, 0, newElements, 0, index); System.arraycopy(elements, index + 1, newElements, index, numMoved); setArray(newElements); } return oldValue; } finally { lock.unlock(); } }
原文地址:https://www.cnblogs.com/cymiao/p/8400397.html
时间: 2024-10-15 23:06:59