Vector学习

vector为list的线程安全版本,通过查看源代码,我们做进一步的学习;

一个是构造方法,源码如下:


可以看出其初始化大小为10;

查看其add方法,源码如下:

  /**
     * Appends the specified element to the end of this Vector.
     *
     * @param e element to be appended to this Vector
     * @return {@code true} (as specified by {@link Collection#add})
     * @since 1.2
     */
    public synchronized boolean add(E e) {
        modCount++;
        ensureCapacityHelper(elementCount + 1);
        elementData[elementCount++] = e;
        return true;
    }


可以看出加锁是通过synchronized来进行加锁的;

学习先暂到这里;










猜你喜欢

转载自blog.csdn.net/hackland2012/article/details/61916551