Vector is the last article in the List collection framework source code analysis series. Unlike ArrayList and LinkedList, Vector is rarely used in projects.

The following is the Vector framework and inheritance, similar to the other two members of the List family, except that it is thread-safe.

public class Vector<E> extends AbstractList<E> implements 
List<E>, RandomAccess.Cloneable.java.io.Serializable
{}Copy the code

Basic knowledge of

Here’s what we should know about vector:

Default capacity: 10

Default value: 0

Basic data structures: arrays

Maximum capacity: 0x7FFFFFFF (integer.max_value) -8

The following is the implementation of the Vector expansion algorithm.

private void grow(int minCapacity) {
		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);
}
Copy the code

Algorithm implementation steps:

  • Get the old capacity
  • New capacity = old capacity + (capacity growth > 0)? Capacity growth: Old capacity
  • If the new capacity – the old capacity is less than 0 the old capacity is used
  • If the new capacity is greater than the default maximum capacity, use the default capacity
  • Finally, call copyof to copy an array to perform expansion

Guarantee multithreading ability: synchronized. Java Vector uses synchronized to ensure that Vector can be used in multi-threaded scenarios.

synchronized

What is synchronized?

Synchronized is a type of synchronization lock that ensures that only one method can enter a critical region at a time, and it also ensures memory visibility of shared variables.

The scope of modification is as follows:

  • Modify a code block;
  • Modify a method;
  • Modify a static method.
  • Modify a class;

The specific functions are not explained in detail in this article.

How do I convert ArrayList and LinkedList to thread-safe types

Here’s the problem: What if your boss insists on using ArrayList or LinkedList for development? How can we solve the problem of thread safety?

Collections.synchronizedList(list);
Copy the code

Using this interface, we can lock any class that inherits the List interface.

synchronized (mutex) {returnlist.equals(o); }Copy the code

Thread safety is implemented internally by a mutex, which I will not analyze here. The detailed source code analysis will be described in the thread safety collection framework section.

conclusion

Collections framework is it after the last chapter of the article will enter to the sections of the Map, in fact, for students to learn about Java List is a Java programmer path to evolve from beginner to advanced, in fact it is not hard to key we need to know its core ideas, such as basic data structures, the default, expansion method, the maximum value, whether the thread safety, etc. And just by focusing on that, you basically have a grasp of how these frameworks are used.