为什么不推荐使用Vector
Vector的底层与ArrayList类似.都是以动态数组的方式进行对象的存储
Vector与ArrayList的区别在于Vector是线程同步操作安全的
Vector的并发安全保证
看Vector的源码(如下)发现很多对外的方法都用Synchronized关键字进行修饰
所以通过vector进行操作性能并不高,因此慢慢被放弃
public synchronized void addElement(E obj) {modCount++;ensureCapacityHelper(elementCount + 1);elementData[elementCount++] = obj;
}public synchronized boolean removeElement(Object obj) {modCount++;int i = indexOf(obj);if (i >= 0) {removeElementAt(i);return true;}return false;
}
public synchronized E get(int index) {if (index >= elementCount)throw new ArrayIndexOutOfBoundsException(index);return elementData(index);
}
如上可知,Vector是线程安全的,但是性能较差,不推荐使用。
在工作中,我们有可能将集合(ArrayList与LinkedList)转换为线程安全。
就要用到Collection工具类
集合转换为线程安全的对象
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
