CopyOnWriteArrayList內部操作
特點
- 多佔用內存: 寫數據時是copy了一份完整的數據,單獨進行操作
- 數據一致性:操作完數據,其他線程不一定能馬上讀取到最新數據 (只能保證最終數據一致性,不能保證數據實時一致)
主要的成員
/**寫時候需要的鎖*/
final transient ReentrantLock lock = new ReentrantLock();
/**數據存儲的位置*/
private transient volatile Object[] array;
構造方法
/**
* 默認是創建一個空的數組
*/
public CopyOnWriteArrayList() {
setArray(new Object[0]);
}
//給定一個數組,需要做數組的拷貝
public CopyOnWriteArrayList(E[] toCopyIn) {
setArray(Arrays.copyOf(toCopyIn, toCopyIn.length, Object[].class));
}
//給定一個集合
public CopyOnWriteArrayList(Collection<? extends E> c) {
Object[] elements;
//需要判斷是否是當前CopyOnWriteArrayList對象
if (c.getClass() == CopyOnWriteArrayList.class)
elements = ((CopyOnWriteArrayList<?>)c).getArray();//如果是直接賦值操作
else {
//將集合轉換成數組
elements = c.toArray();
// c.toArray might (incorrectly) not return Object[] (see 6260652)
//如果不是Object數組進行數組拷貝
if (elements.getClass() != Object[].class)
elements = Arrays.copyOf(elements, elements.length, Object[].class);
}
//賦值數組
setArray(elements);
}
add()方法
- 加鎖處理
- 將數組拷貝到一個新的數組(長度爲原長度+1)上
- 將要添加的元素放在數組最後一個元素上
- 重新賦值數組
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();//釋放鎖
}
}
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();//釋放鎖
}
}
addIfAbsent() 如果不存在就添加
public boolean addIfAbsent(E e) {
Object[] snapshot = getArray();//獲取了一個快照
return indexOf(e, snapshot, 0, snapshot.length) >= 0 ? false :
addIfAbsent(e, snapshot);
}
private boolean addIfAbsent(E e, Object[] snapshot) {
final ReentrantLock lock = this.lock;
lock.lock();//獲取鎖
try {
Object[] current = getArray();
int len = current.length;
if (snapshot != current) {
// Optimize for lost race to another addXXX operation
//獲取當前和快照中數組長度的最小值
int common = Math.min(snapshot.length, len);
for (int i = 0; i < common; i++)
//如果要添加的元素已經存在了
if (current[i] != snapshot[i] && eq(e, current[i]))
return false;
//要添加的元素已存在
if (indexOf(e, current, common, len) >= 0)
return false;
}
//數組拷貝
Object[] newElements = Arrays.copyOf(current, len + 1);
newElements[len] = e;//賦值
setArray(newElements);
return true;
} finally {
lock.unlock();//釋放鎖
}
}
set()方法
//有鎖操作
public E set(int index, E element) {
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] elements = getArray();
E oldValue = get(elements, index);
if (oldValue != element) {//要設置的元素和原來的元素不同
int len = elements.length;
Object[] newElements = Arrays.copyOf(elements, len);
newElements[index] = element;//設置指定索引的元素
setArray(newElements);
} else {
// Not quite a no-op; ensures volatile write semantics
setArray(elements);
}
return oldValue;
} finally {
lock.unlock();
}
}
get()方法
public E get(int index) {
return get(getArray(), index);
}
private E get(Object[] a, int index) {
return (E) a[index];//直接從數組中返回
}
PS : 該類適應在寫多讀少的場景