Vector
1 2 3 4 5
| public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable
|
Vector中的操作是线程安全的。
Vector加synchronized锁后真的是线程安全?不是,因为锁的是方法,锁的粒度不一定是线程安全的。
复合操作需要程序自己控制线程安全
与ArrayList不同的地方:
所有方法都加了synchronized关键字
Vector没有空数组的设计
1 2 3
| ArrayList: private static final Object[] EMPTY_ELEMENTDATA = {}; private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
|
- 扩容因子不一样,Vector的扩容因子是2,ArrayList的扩容因子是1.5
1 2 3
| ArrayList: int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1);
|
1 2 3 4
| Vector: int oldCapacity = elementData.length; int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity);
|