Vector源码分析

ArrayList很类似,都是以动态数组的形式来存储数据

Vector线程安全的

每个操作方法都加的有synchronized关键字,针对性能来说会比较大的影响,慢慢就被放弃了

Collections工具类

可以增加代码的灵活度,在我们需要同步的时候,通过Collections转化一下就好

// 对应不是线程不安全的普通list
// 直接用工具转成安全的就行,方便
// 实质也是 synchronized
List syncList = Collections.synchronizedList(list);
// 本质就是把所有的方法加 synchronized
public E get(int index) {
     synchronized (mutex) {return list.get(index);}
 }
public E set(int index, E element) {
    synchronized (mutex) {return list.set(index, element);}
}
public void add(int index, E element) {
    synchronized (mutex) {list.add(index, element);}
}
public E remove(int index) {
    synchronized (mutex) {return list.remove(index);}
}
原文地址:https://www.cnblogs.com/laoyin666/p/13976351.html