CopyOnWriteArrayList集合線程安全解釋
阿新 • • 發佈:2018-02-01
ets lean style cif element int this ofb out
高並發操作共享的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(); } }
CopyOnWriteArrayList集合線程安全解釋