注:玩的是JDK1.7版本html
一: 先上类图,从类图上看和 ArrayList.java 很是相像,可查看 分析轮子(一)-ArrayList.javajava
二:而后看源码,发现和 ArrayList.java 各类实现上也很是的相像,他们的底层数据结构都是数组,而且均可以动态扩展,因此,再也不重述了,有兴趣能够查看 分析轮子(一)-ArrayList.java数组
/** * The array buffer into which the components of the vector are * stored. The capacity of the vector is the length of this array buffer, * and is at least large enough to contain all the vector's elements. * * <p>Any array elements following the last element in the Vector are null. * * @serial */ protected Object[] elementData;
三:如此相似的类为何须要两个呢?写JDK源码的人怎么可能犯这个错误,来看看他们的不一样点吧!安全
1)当须要扩展空间时,ArrayList.java 增长原来的一半 Vector.java 增长原来的一倍数据结构
2)ArrayList.java 非线程安全 Vector.java 是线程安全post
Vector.java 类的许多方法都使用了 synchronized 同步关键字来修饰了,表明 Vector.java 是一个线程安全的类!固然,对应的代价嘛!就是开销相对会大一些。加锁后为何开销会大一些呢?this
想像一下,你在北京天安门附近有一座四合院,大门、小门、卧室门你都上锁了,你晚上写完代码,下班后到家休息的时候,是否是要一个个得到锁,而后一个个的开锁,才能进门休息,是否是有点费事费时呢?url