Vector 是一个古老的容器,它与 ArrayList 一样,都是 list 的动态数组实现,底层的数据结构是一个数组,对其进行操作则转化为对数组进行操作。另外,在数据长度不够时,还可指定要扩容多少,若没指定,则默认扩容为原来的 2 倍。
Vector 继承于 AbstractList,其与 ArrayList 一样都拥有对集合进行增删查改的操作,且操作过程也一样,唯一不同的地方时,Vector 调用方法时会使用 synchronized 来对 Vector 对象进行加锁,因为加锁了,在一个时刻只能有一个线程修改 Vector,因此避免了线程安全问题。下面来看看 Vector 的各个方法实现:
添加元素到集合中
public synchronized boolean add(E e) { modCount++; ensureCapacityHelper(elementCount + 1); elementData[elementCount++] = e; return true;} public synchronized void insertElementAt(E obj, int index) { modCount++; if (index > elementCount) { throw new ArrayIndexOutOfBoundsException(index + " > " +elementCount); } ensureCapacityHelper(elementCount + 1); System.arraycopy(elementData, index, elementData, index + 1, elementCount - index); elementData[index] = obj; elementCount++;}从两个方法定义就可以知道,其每个方法都使用了 synchronized 关键字来定义,从而保证每个线程要调用该方法时,必须要获得该对象的对象锁才能调用,否则则会进入阻塞,等待锁资源。 另外,在每次添加元素时,都需要测试当前数组长度,若当前已经不能容纳元素时,则会触发扩容操作,扩容操作也是线程安全的。
PRivate void grow(int minCapacity) { int oldCapacity = elementData.length; // 如果没指定扩容增量,则扩容为原来的 2 倍 int newCapacity = oldCapacity + ((capacityIncrement > 0) ?capacityIncrement : oldCapacity); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); elementData = Arrays.copyOf(elementData, newCapacity);}将元素从集合移除,该方法同样有两种方式,前者删除 index 位置上的元素,后者是删除与 obj 元素相同的第一个元素。
public synchronized E remove(int index) { modCount++; if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(index); E oldValue = elementData(index); int numMoved = elementCount - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index,numMoved); // 该操作是为了保证 GC 能够正常工作 elementData[--elementCount] = null; return oldValue;}public synchronized boolean removeElement(Object obj) { modCount++; int i = indexOf(obj); if (i >= 0) { removeElementAt(i); return true; } return false;}public synchronized int indexOf(Object o, int index) { if (o == null) { for (int i = index ; i < elementCount ; i++) if (elementData[i]==null) return i; } else { for (int i = index ; i < elementCount ; i++) if (o.equals(elementData[i])) return i; } return -1;}Vector 的遍历有两种方式,一种是获取 Iterator,另一种是获取 Enumeration,前者可用于遍历集合并且删除集合的元素;后者只能遍历集合。
Iterator Vector 中 的 Iterator 实现其实和 ArrayList 的实现是一样的,唯一不同的地方是它在其基础上,要进行移动和删除的操作时,首先需要获取对象锁,保证同一个时刻只有一个线程在遍历集合。
private class Itr implements Iterator<E> { int cursor; int lastRet = -1; int expectedModCount = modCount; public boolean hasNext() { return cursor != elementCount; } public E next() { synchronized (Vector.this) { checkForComodification(); int i = cursor; if (i >= elementCount) throw new NoSuchElementException(); cursor = i + 1; return elementData(lastRet = i); } } public void remove() { if (lastRet == -1) throw new IllegalStateException(); synchronized (Vector.this) { checkForComodification(); Vector.this.remove(lastRet); expectedModCount = modCount; } cursor = lastRet; lastRet = -1; } final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); } }}Enumeration Enumeration 只可用于遍历元素。
public Enumeration<E> elements() { return new Enumeration<E>() { int count = 0; public boolean hasMoreElements() { return count < elementCount; } public E nextElement() { synchronized (Vector.this) { if (count < elementCount) { return elementData(count++); } } throw new NoSuchElementException("Vector Enumeration"); } };}Vector 和 ArrayList 都是 list 的动态数组实现,他们都继承于 AbstractList,实现了都同时实现增删查改操作。但 Vector 它是线程安全,而 ArrayList 是线程不安全的,Vector 每个对集合的方法操作都需要先申请集合的对象锁,这样可以保证在同一时刻只有一个线程能操作集合,若当前锁被占用,申请不了锁,则线程只能处于阻塞状态,等待资源调度器的唤醒。也正由于 Vector 是线程同步,因此并发的性能比较低下。
他们的区别主要有以下两点:
Vector 是线程安全的,ArrayList 是线程不安全的Vector 扩容时会扩大成原来的 2 倍(在没指定扩容增量时),ArrayList 扩容则是原来的 1.5 倍新闻热点
疑难解答