jdk version:1.8

成员变量

  1. protected Object[] elementData;
  2. protected int elementCount;
  3. protected int capacityIncrement;

capacityIncrement 为每次扩容时自定义增加的容量值

构造方法

  1. public Vector(int initialCapacity, int capacityIncrement) {
  2. super();
  3. if (initialCapacity < 0)
  4. throw new IllegalArgumentException("Illegal Capacity: "+
  5. initialCapacity);
  6. //capacityIncrement 为每次扩容时自定义增加的容量值
  7. this.elementData = new Object[initialCapacity];
  8. this.capacityIncrement = capacityIncrement;
  9. }
  10. public Vector(int initialCapacity) {
  11. this(initialCapacity, 0);
  12. }
  13. public Vector() {
  14. //默认容量大小为 10
  15. this(10);
  16. }
  17. public Vector(Collection<? extends E> c) {
  18. elementData = c.toArray();
  19. elementCount = elementData.length;
  20. // c.toArray might (incorrectly) not return Object[] (see 6260652)
  21. if (elementData.getClass() != Object[].class)
  22. elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
  23. }

add

  1. public synchronized boolean add(E e) {
  2. //加了 synchronized 锁,是线程安全的。
  3. modCount++;
  4. ensureCapacityHelper(elementCount + 1);
  5. elementData[elementCount++] = e;
  6. return true;
  7. }
  8. private void ensureCapacityHelper(int minCapacity) {
  9. // overflow-conscious code
  10. if (minCapacity - elementData.length > 0)
  11. grow(minCapacity);
  12. }
  13. private void grow(int minCapacity) {
  14. // overflow-conscious code
  15. int oldCapacity = elementData.length;
  16. //扩容量:如果没有指定每次的扩容量,则翻倍
  17. int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
  18. capacityIncrement : oldCapacity);
  19. if (newCapacity - minCapacity < 0)
  20. newCapacity = minCapacity;
  21. if (newCapacity - MAX_ARRAY_SIZE > 0)
  22. newCapacity = hugeCapacity(minCapacity);
  23. elementData = Arrays.copyOf(elementData, newCapacity);
  24. }
  25. private static int hugeCapacity(int minCapacity) {
  26. if (minCapacity < 0) // overflow
  27. throw new OutOfMemoryError();
  28. return (minCapacity > MAX_ARRAY_SIZE) ?
  29. Integer.MAX_VALUE :
  30. MAX_ARRAY_SIZE;
  31. }

没啥好说的,和 ArrayList 类似,只不过每次最小扩容量可以自定义,如果没有自定义,就是每次都最小扩容翻倍。

get、set、remove

  1. public synchronized E get(int index) {
  2. if (index >= elementCount)
  3. throw new ArrayIndexOutOfBoundsException(index);
  4. return elementData(index);
  5. }
  1. public synchronized E set(int index, E element) {
  2. if (index >= elementCount)
  3. throw new ArrayIndexOutOfBoundsException(index);
  4. E oldValue = elementData(index);
  5. elementData[index] = element;
  6. return oldValue;
  7. }
  1. public synchronized E remove(int index) {
  2. modCount++;
  3. if (index >= elementCount)
  4. throw new ArrayIndexOutOfBoundsException(index);
  5. E oldValue = elementData(index);
  6. int numMoved = elementCount - index - 1;
  7. if (numMoved > 0)
  8. System.arraycopy(elementData, index+1, elementData, index,
  9. numMoved);
  10. elementData[--elementCount] = null; // Let gc do its work
  11. return oldValue;
  12. }

以上三个操作方法都没啥说的,代码一目了然,和 ArrayList 相比,每个方法都加了 synchronized,是线程安全的。

Stack

stack 先进后出。继承与 Vector,所以是线程安全的。本身代码不多。

  1. class Stack<E> extends Vector<E> {
  2. public Stack() {
  3. }
  4. public E push(E item) {
  5. //添加元素
  6. addElement(item);
  7. return item;
  8. }
  9. public synchronized E pop() {
  10. E obj;
  11. int len = size();
  12. obj = peek();
  13. removeElementAt(len - 1);
  14. return obj;
  15. }
  16. public synchronized E peek() {
  17. int len = size();
  18. if (len == 0)
  19. throw new EmptyStackException();
  20. //获取最后一个元素
  21. return elementAt(len - 1);
  22. }
  23. public boolean empty() {
  24. return size() == 0;
  25. }
  26. public synchronized int search(Object o) {
  27. int i = lastIndexOf(o);
  28. if (i >= 0) {
  29. return size() - i;
  30. }
  31. return -1;
  32. }
  33. /** use serialVersionUID from JDK 1.0.2 for interoperability */
  34. private static final long serialVersionUID = 1224463164541339165L;
  35. }

addElement

也是线程安全的

  1. public synchronized void addElement(E obj) {
  2. modCount++;
  3. ensureCapacityHelper(elementCount + 1);
  4. elementData[elementCount++] = obj;
  5. }

size

也是线程安全的

  1. public synchronized int size() {
  2. return elementCount;
  3. }

elementAt

也是线程安全的

  1. public synchronized E elementAt(int index) {
  2. if (index >= elementCount) {
  3. throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
  4. }
  5. return elementData(index);
  6. }

总结

所以 stack 的 push、pop、peek、empty、search 都是线程安全的。
stack 的先进后出是,往末尾添加元素,再从末尾取出元素。