Vector源码解析

本文是基于JDK1.8源码分析。Vector和ArrayList非常类似,继承相同的类,实现相同的接口,就连方法实现也基本类似。最大的不同:ArrayList非线程安全,而Vector是线程安全的,关键方法都加了synchronized同步锁,因此Vector的效率不高,要使用线程安全的ArrayList,推荐使用CopyOnWriteArrayList。后续单独分析

1.类结构

Vector类层级结构如下图所示:

Vector包含的成员变量:

    //elementData为Object类型的数组,用于存储数据
    protected Object[] elementData; 
    //表示数组元素个数(不是数组容量)
    protected int elementCount;
    //扩容时增加容量数,数组容量不足时,如果capacityIncrement<=0,则扩容2倍,否则容量增加capacityIncrement
    protected int capacityIncrement;

2.方法解析
2.1构造函数
public Vector(int initialCapacity, int capacityIncrement) {
        super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        //创建指定容量的数组
        this.elementData = new Object[initialCapacity];
        //增加容量数capacityIncrement赋值为指定值
        this.capacityIncrement = capacityIncrement;
    }

public Vector(int initialCapacity) {
        //创建指定容量的数组
        this(initialCapacity, 0);
    }

public Vector() {
        //创建容量为10的数组
        this(10);
    }

当我们调用new Vector()创建Vector集合时 ,直接创建了一个容量为10的对象数据(和ArrayList不同,ArrayList调用无参构造函数时,内部初始化数组容量为0,只有添加第一个元素时才扩容为10),由于capacityIncrement为0,意味着数组容量不足时,新数组容量为旧数组容量的2倍。

2.2  add(E e)

add(E e)用于尾部添加元素:

public synchronized boolean add(E e) {
        modCount++;
        ensureCapacityHelper(elementCount + 1);
        elementData[elementCount++] = e;
        return true;
    }

private void ensureCapacityHelper(int minCapacity) {
        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }

private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        //capacityIncrement>0,新容量newCapacity为旧容量+capacityIncrement,否则,新容量newCapacity为旧容量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);
    }

private static int hugeCapacity(int minCapacity) {
        if (minCapacity < 0) // overflow
            throw new OutOfMemoryError();
        return (minCapacity > MAX_ARRAY_SIZE) ?
            Integer.MAX_VALUE :
            MAX_ARRAY_SIZE;
    }

添加逻辑和ArrayList的add方法大体一致,区别在于扩容策略有些不同,并且add方法使用synchronized修饰。

2.3 remove(int index)

remove(int index) 删除指定位置元素

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);
        elementData[--elementCount] = null; // Let gc do its work

        return oldValue;
    }

逻辑和ArrayList的remove方法一致,方法使用synchronized关键字修饰

2.4 get(int index)
get(int index)获取指定位置元素
public synchronized E get(int index) {
        if (index >= elementCount)
            throw new ArrayIndexOutOfBoundsException(index);

        return elementData(index);
    }

E elementData(int index) {
        return (E) elementData[index];
    }

逻辑和ArrayList的get方法一致,方法使用synchronized关键字修饰。

2.5 set(int index, E element)
set(int index, E element)设置指定位置的元素为指定值
public synchronized E set(int index, E element) {
        if (index >= elementCount)
            throw new ArrayIndexOutOfBoundsException(index);

        E oldValue = elementData(index);
        elementData[index] = element;
        return oldValue;
    }

逻辑和ArrayList的set方法一致,方法使用synchronized关键字修饰。

剩下的方法可自己查看源码,大体和ArrayList没什么区别。Vector的方法都是使用synchronized关键字来实现线程安全,每次只能一个线程访问次对象,在线程竞争激烈的情况下,效率非常强,实际不推荐使用Vector。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值