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来进行加锁的;
学习先暂到这里;
本文解析了Java中Vector类的源码,重点介绍了Vector的构造方法及add方法,并详细解释了其实现线程安全的方式。
1111

被折叠的 条评论
为什么被折叠?



