You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
public class CopyOnWriteArrayList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable {
private static final long serialVersionUID = 8673264195747942595L;
/** The lock protecting all mutators */
final transient ReentrantLock lock = new ReentrantLock();
/** The array, accessed only via getArray/setArray. */
private transient volatile Object[] array;
public CopyOnWriteArrayList() {
setArray(new Object[0]);
}
public E get(int index) {
return get(getArray(), index);
}
//添加时,加锁,重新copy一份数组,用新数组覆盖原来的数组
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();
}
}
CopyOnWriteArrayList是线程安全的List,数据结构是数组,线程安全体现在每次修改会重新copy一份数据。
思考一下,为什么add时要重新copy一份数组,只加锁不行吗?如果只加锁不copy,那就相当于变成了ArrayList修改时加锁,读取时不加锁,假如有一个线程在for循环,另一个线程在修改,就会产生ConcurrentModificationException。
The text was updated successfully, but these errors were encountered: