文章目录
- 简介
- 为什么是线程安全的?
- 增
- 删
- 改
- 查
- 扩容
- 总结
简介
Java集合体系中的一个线性集合,底层原理其实和ArrayList基本一致。关于Vector问到的最多的面试题:ArrayList和Vector又什么区别?
通常的回答:
- Vector是线程安全的
- 扩容机制不同,ArrayList通常增长原本容量的0.5倍,vector增长1倍
本篇文章就通过这个面试题,结合Vector的源码验证一下
为什么是线程安全的?
通过翻看Vector的源码,发现Vector对外提供的对集合进行操作的函数都是通过synchronized关键字进行加锁的。这也就是为啥说Vector是线程安全的。
随便看几个函数:
增
public synchronized boolean add(E e) {modCount++;add(e, elementData, elementCount);return true;}
删
public synchronized boolean removeElement(Object obj) {modCount++;int i = indexOf(obj);if (i >= 0) {removeElementAt(i);return true;}return false;}
改
public synchronized E set(int index, E element) {if (index >= elementCount)throw new ArrayIndexOutOfBoundsException(index);E oldValue = elementData(index);elementData[index] = element;return oldValue;}
查
public synchronized E get(int index) {if (index >= elementCount)throw new ArrayIndexOutOfBoundsException(index);return elementData(index);}
扩容
private Object[] grow(int minCapacity) {int oldCapacity = elementData.length;int newCapacity = ArraysSupport.newLength(oldCapacity,minCapacity - oldCapacity, /* minimum growth */capacityIncrement > 0 ? capacityIncrement : oldCapacity/* preferred growth */);return elementData = Arrays.copyOf(elementData, newCapacity);}
观察源码可以发现,和ArrayList中的源码只有ArraysSupport.newLength的第三个参数不同,如果在构造Vector的时候没有指定capacityIncrement,则该值被初始化为0,那每一次第三个参数都将被传入oldCapacity
所以如果我所需要的最小增量小于原容量,按照一倍扩容;否则按照申请的大小扩容,但是不能超过SOFT_MAX_ARRAY_LENGTH
这里的细节不再追究,以后遇到再说吧,因为Vector本身也用的很少。
总结
相较于ArrayList,Vector通过对方法进行加锁的方式实现了线程安全,所以性能会有所下降,但是是基于不同场景下的,所以这样比较也没啥意义。
总之,应该在合适的场景下选择合适的数据结构,避免频繁的扩容有利于提升程序的运行时性能。