/** The lock protecting all mutators */ final transient ReentrantLock lock = new ReentrantLock();
/** The array, accessed only via getArray/setArray. */ private transient volatile Object[] array;
主要的成员变量有2个,一个ReentrantLock,一个是内部存值的数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/** * 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(); }
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;