[JDK] JDK source code analysis -Vector

Outline

 

Above " JDK source code analysis -ArrayList " analyzes the realization of the principle of ArrayList. This paper analyzes another class that implements the List interface: Vector.

 

Vector implementation and internal ArrayList similar, can also be understood as a "variable array." Which follows the inheritance structure (not part of the interface):

PS: As the Vector currently use less, and the government has also recommended ArrayList instead of Vector in the process of wireless security requirements, just to study its implementation principle.

 

stackoverflow also related to the discussion:

https://stackoverflow.com/questions/1386275/why-is-java-vector-and-stack-class-considered-obsolete-or-deprecated

 

Still be analyzed from the constructor to start. 

 

Constructor

 

Vector Foreign offers four constructors (interior can be considered two), one:

protected Object [] elementData of; 

protected  int of capacityIncrement; 

// no argument constructor 
public the Vector () {
     the this (10 ); 
} 

// specified capacity constructor 
public the Vector ( int initialCapacity) {
     the this (initialCapacity, 0 ); 
} 

/ / specified initial capacity and capacity growth factor constructors 
public the Vector ( int initialCapacity, int of capacityIncrement) {
     Super ();
     IF (initialCapacity <0 )
         the throw  new new IllegalArgumentException("Illegal Capacity: "+
                                           initialCapacity);
    this.elementData = new Object[initialCapacity];
    this.capacityIncrement = capacityIncrement;
}        

And ArrayList Similarly, Vector maintained an internal array of type Object (elementData of) the element to store (default capacity is 10). The difference is: Vector more than one parameter capacityIncrement ArrayList constructor, the variable is also led to a slightly different way both expansion and analyzed later.

 

Second: the set of parameters for the constructor

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);
}

 

Expansion Principle Analysis

 

We still start from the add () method for analysis:

public synchronized boolean add(E e) {
    modCount++;
    ensureCapacityHelper(elementCount + 1);
    elementData[elementCount++] = e;
    return true;
}   

Note synchronized keyword here. Observation can be found: Many methods are used in the internal Vector keyword, which is thread-safe manner Vector realized, simple and crude!

 

Expansion methods which achieve the following:

/ ** 
 * Components of The Number of Valid in the this { @code the Vector Object}. 
 * Components elementData of [0] through 
 * elementData of [-to the elementCount. 1] The Actual are items. 
 * / 
Protected  int to the elementCount; 

/ * 
 * This method is asynchronous a 
 * Vector as local calls the internal method are used to synchronize the synchronized keyword, there is no longer additional use 
 * / 
Private  void ensureCapacityHelper ( int be minCapacity) {
     // overflow-Conscious code
     // further expansion operation is greater than the capacity of the array 
    IF (be minCapacity - elementData.length> 0 ) 
        Grow (be minCapacity); 
} 

Private  void Grow (int minCapacity) {
    // overflow-conscious code
    int oldCapacity = elementData.length;
    int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity);
    if (newCapacity - minCapacity < 0)
        newCapacity = minCapacity;
    if (newCapacity - MAX_ARRAY_SIZE > 0)
        newCapacity = hugeCapacity(minCapacity);
    elementData = Arrays.copyOf(elementData, newCapacity);
}

private static int hugeCapacity(int minCapacity) {
    if (minCapacity < 0) // overflow
        throw new OutOfMemoryError();
    return (minCapacity > MAX_ARRAY_SIZE) ?
        Integer.MAX_VALUE :
        MAX_ARRAY_SIZE;
}                

 

Can be seen, consistent with the Vector ArrayList expansion mode, only the new capacity is calculated differently, here at its new capacity size analysis:

int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity);

 

When calculating the new Vector capacity after expansion, depending on the value of capacityIncrement can be divided into two cases:

 

1. capacityIncrement> 0: new = old capacity Capacity + capacityIncrement;

 

2. capacityIncrement <= 0: New Old Capacity * Capacity = 2.

 

Thread safety

 

Vector is thread-safe, thread-safe way to achieve it is also very simple and crude: the use of the synchronized keyword directly on the method for synchronization.

 

Vector Summary

 

1. Similar to the ArrayList, Vector can also be considered a "variable array";

 

2. Principles and expansion ArrayList basically the same, but new capacity is calculated slightly differently: When specifying increase capacity, new capacity growth for the old capacity + capacity; otherwise the expansion is twice the old capacity;

 

3. Thread safe implementation of simple (synchronized);

 

4. The current use less, just to learn the principles of its implementation. 

 

Stay hungry, stay foolish.

PS: This article first appeared from the public micro-channel number.

Guess you like

Origin www.cnblogs.com/jaxer/p/11105444.html