标签:
今天开始,看一下JDK容器源码,这要比比其他jdk源码要简单的多,大部分都能看的懂,这里就不在多言。重点是:
Vector的扩容机制:
若自动增长量小于0,则新长度为当前长度的两倍,否则为旧容量+capacityIncrement
和线程安全的原因就是因为使用了同步控制 synchronized。
public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable { //底层实现 protected Object[] elementData; protected int elementCount; //容量增长量 protected int capacityIncrement; public Vector(int initialCapacity, int capacityIncrement) { super(); if (initialCapacity < 0) throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); this.elementData = new Object[initialCapacity]; this.capacityIncrement = capacityIncrement; } public Vector(int initialCapacity) { this(initialCapacity, 0); } public Vector() { this(10); }
<span style="font-family: Arial, Helvetica, sans-serif;">//c.toArray()可能返回一个非Object类型的数组,所以使用Arrays.copyOf创建一个Object[].class类型数组</span><pre name="code" class="java">//扩容机制:若自动增长量小于0则新长度为当前长度的两倍,否则旧容量+capacityIncrement <pre name="code" class="java">//然后在调用 System.arraycopy,并且返回Object数组,
public Vector(Collection<? extends E> c) { elementData = c.toArray(); elementCount = elementData.length; // c.toArray might (incorrectly) not return Object[] (see 6260652) if (elementData.getClass() != Object[].class) elementData = Arrays.copyOf(elementData, elementCount, Object[].class); } public synchronized void copyInto(Object[] anArray) { System.arraycopy(elementData, 0, anArray, 0, elementCount); } //去掉没用(无值)的尾部 public synchronized void trimToSize() {</span>
<span style="font-size:18px;"> //父类AbstractList中的成员域,fail-fast机制支持 modCount++; int oldCapacity = elementData.length; if (elementCount < oldCapacity) { elementData = Arrays.copyOf(elementData, elementCount); } } //扩容 public synchronized void ensureCapacity(int minCapacity) { modCount++; ensureCapacityHelper(minCapacity); } //参数为数组的新长度 private void ensureCapacityHelper(int minCapacity) { int oldCapacity = elementData.length; if (minCapacity > oldCapacity) { Object[] oldData = elementData; int newCapacity = (capacityIncrement > 0) ? (oldCapacity + capacityIncrement) : (oldCapacity * 2); if (newCapacity < minCapacity) { newCapacity = minCapacity; } elementData = Arrays.copyOf(elementData, newCapacity); } } public synchronized void setSize(int newSize) { modCount++; if (newSize > elementCount) { ensureCapacityHelper(newSize); } else { for (int i = newSize ; i < elementCount ; i++) { elementData[i] = null; } } elementCount = newSize; } public synchronized int capacity() { return elementData.length; } public synchronized int size() { return elementCount; } public synchronized boolean isEmpty() { return elementCount == 0; } //返回一个迭代的</span><span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif;">Enumeration</span><span style="font-size:18px;"> public Enumeration<E> elements() { return new Enumeration<E>() { int count = 0; public boolean hasMoreElements() { return count < elementCount; } public E nextElement() { synchronized (Vector.this) { if (count < elementCount) { return (E)elementData[count++]; } } throw new NoSuchElementException("Vector Enumeration"); } }; } public boolean contains(Object o) { return indexOf(o, 0) >= 0; } public int indexOf(Object o) { return indexOf(o, 0); } public synchronized int indexOf(Object o, int index) { if (o == null) { for (int i = index ; i < elementCount ; i++) if (elementData[i]==null) return i; } else { for (int i = index ; i < elementCount ; i++) if (o.equals(elementData[i])) return i; } return -1; } public synchronized int lastIndexOf(Object o) { return lastIndexOf(o, elementCount-1); } public synchronized int lastIndexOf(Object o, int index) { if (index >= elementCount) throw new IndexOutOfBoundsException(index + " >= "+ elementCount); if (o == null) { for (int i = index; i >= 0; i--) if (elementData[i]==null) return i; } else { for (int i = index; i >= 0; i--) if (o.equals(elementData[i])) return i; } return -1; } public synchronized E elementAt(int index) { if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); } return (E)elementData[index]; } public synchronized E firstElement() { if (elementCount == 0) { throw new NoSuchElementException(); } return (E)elementData[0]; } public synchronized E lastElement() { if (elementCount == 0) { throw new NoSuchElementException(); } return (E)elementData[elementCount - 1]; } public synchronized void setElementAt(E obj, int index) { if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); } elementData[index] = obj; } //每次改变数组结构时modCount都会+1 public synchronized void removeElementAt(int index) { modCount++; if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); } else if (index < 0) { throw new ArrayIndexOutOfBoundsException(index); } int j = elementCount - index - 1; if (j > 0) { System.arraycopy(elementData, index + 1, elementData, index, j); } elementCount--; elementData[elementCount] = null; } public synchronized void insertElementAt(E obj, int index) { modCount++; if (index > elementCount) { throw new ArrayIndexOutOfBoundsException(index + " > " + elementCount); } ensureCapacityHelper(elementCount + 1); System.arraycopy(elementData, index, elementData, index + 1, elementCount - index); elementData[index] = obj; elementCount++; } public synchronized void addElement(E obj) { modCount++; ensureCapacityHelper(elementCount + 1); elementData[elementCount++] = obj; } public synchronized boolean removeElement(Object obj) { modCount++; int i = indexOf(obj); if (i >= 0) { removeElementAt(i); return true; } return false; } public synchronized void removeAllElements() { modCount++; for (int i = 0; i < elementCount; i++) elementData[i] = null; elementCount = 0; } //注意java的克隆都是浅复制,若复制一个对象,java clone新开辟一个对象的空间,但是类中引用还是指向原来的地址,</span>
<span style="font-size:18px;">//并未重新开辟空间 public synchronized Object clone() { try { Vector<E> v = (Vector<E>) super.clone(); v.elementData = Arrays.copyOf(elementData, elementCount); v.modCount = 0; return v; } catch (CloneNotSupportedException e) { // this shouldn't happen, since we are Cloneable throw new InternalError(); } } public synchronized Object[] toArray() { return Arrays.copyOf(elementData, elementCount); } public synchronized <T> T[] toArray(T[] a) { if (a.length < elementCount) return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass()); System.arraycopy(elementData, 0, a, 0, elementCount); if (a.length > elementCount) a[elementCount] = null; return a; } // Positional Access Operations public synchronized E get(int index) { if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(index); return (E)elementData[index]; } public synchronized E set(int index, E element) { if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(index); Object oldValue = elementData[index]; elementData[index] = element; return (E)oldValue; } public synchronized boolean add(E e) { modCount++; ensureCapacityHelper(elementCount + 1); elementData[elementCount++] = e; return true; } public boolean remove(Object o) { return removeElement(o); } public void add(int index, E element) { insertElementAt(element, index); } public synchronized E remove(int index) { modCount++; if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(index); Object 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 (E)oldValue; } public void clear() { removeAllElements(); } //是否包含所有的元素,一个元素一个元素的检查 public synchronized boolean containsAll(Collection<?> c) { return super.containsAll(c); } public synchronized boolean addAll(Collection<? extends E> c) { modCount++; Object[] a = c.toArray(); int numNew = a.length; ensureCapacityHelper(elementCount + numNew); System.arraycopy(a, 0, elementData, elementCount, numNew); elementCount += numNew; return numNew != 0; } public synchronized boolean removeAll(Collection<?> c) { return super.removeAll(c); } //仅保留此 collection 中那些也包含在指定 collection 的元素 public synchronized boolean retainAll(Collection<?> c) { return super.retainAll(c); } public synchronized boolean addAll(int index, Collection<? extends E> c) { modCount++; if (index < 0 || index > elementCount) throw new ArrayIndexOutOfBoundsException(index); Object[] a = c.toArray(); int numNew = a.length; ensureCapacityHelper(elementCount + numNew); int numMoved = elementCount - index; if (numMoved > 0) System.arraycopy(elementData, index, elementData, index + numNew, numMoved); System.arraycopy(a, 0, elementData, index, numNew); elementCount += numNew; return numNew != 0; } public synchronized boolean equals(Object o) { return super.equals(o); } //hashCoded的计算方式 hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());</span>
<span style="font-size:18px;"> public synchronized int hashCode() { return super.hashCode(); } public synchronized String toString() { return super.toString(); } public synchronized List<E> subList(int fromIndex, int toIndex) { return Collections.synchronizedList(super.subList(fromIndex, toIndex), this); } protected synchronized void removeRange(int fromIndex, int toIndex) { modCount++; int numMoved = elementCount - toIndex; System.arraycopy(elementData, toIndex, elementData, fromIndex, numMoved); // Let gc do its work int newElementCount = elementCount - (toIndex-fromIndex); while (elementCount != newElementCount) elementData[--elementCount] = null; } private synchronized void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { s.defaultWriteObject(); } } </span>
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/jiaomingliang/article/details/47430851