// volatile 的作用是保证读操作的可见性
private transient volatile Object[] array;
public boolean add(E e) {
synchronized (lock) {
Object[] es = this.array;
int len = es.length;
es = Arrays.copyOf(es, len + 1); # 复制数组
es[len] = e;
this.array = a;
return true;
}
}
static final class COWIterator<E> implements ListIterator<E> {
/** Snapshot of the array */
private final Object[] snapshot;
/** Index of element to be returned by subsequent call to next. */
private int cursor;
COWIterator(Object[] es, int initialCursor) {
cursor = initialCursor;
snapshot = es;
}
public void remove() {
throw new UnsupportedOperationException();
}
...
}