Why Java Vector is not used

Introduction

In C ++, std :: vector is used for variable length arrays, but in Java, java.util.ArrayList is usually used, and java.util.Vector, which seems to have similar functions, is used. not.

--It seems to be slow because it synchronizes --If you want a synchronized list, you should use java.util.Collections.synchronizedList ()

I didn't care about it until now because I thought it might be the case when I heard a story like this, but I suddenly became interested, so I took a look at the OpenJDK source.

Vector methods

Certainly each method has a synchronized clause. You should definitely use ʻArrayList` when you don't need synchronization.

Vector.java


...
    public synchronized E get(int index) {
        if (index >= elementCount)
            throw new ArrayIndexOutOfBoundsException(index);

        return elementData(index);
    }
...
    public synchronized boolean add(E e) {
        modCount++;
        add(e, elementData, elementCount);
        return true;
    }
...

Differences from Collections.SynchronizedList

So what is the difference between Vector and Collections.SynchronizedList (new ArrayList <> ()) wrapped with ʻArrayList? What is different at first glance is the behavior of Iterator. In Vector`, the Iterator method also acquires the lock like other methods.

Vector.java


    private class Itr implements Iterator<E> {
...
        public E next() {
            synchronized (Vector.this) {
                checkForComodification();
                int i = cursor;
                if (i >= elementCount)
                    throw new NoSuchElementException();
                cursor = i + 1;
                return elementData(lastRet = i);
            }
        }
...

For SynchronizedCollections inherited by SyncronizedList, Iterator () returns the wrapped type as is.

Collections.java


    static class SynchronizedCollection<E> implements Collection<E>, Serializable {
        private static final long serialVersionUID = 3053995032091335093L;

        final Collection<E> c;  // Backing Collection
        final Object mutex;     // Object on which to synchronize
...
        public Iterator<E> iterator() {
            return c.iterator(); // Must be manually synched by user!
        }

When iterating over a list, it's not enough to get the lock only within a single method, and after all you have to lock the entire iterative process, do it yourself ("Must be manually synched"). by user! ") Maybe that means.

Summary

--Vector has synchronized for each method --The behavior of iterator is different from SynchronizedList

Recommended Posts

Why Java Vector is not used
Java Calendar is not a singleton.
Three reasons why wrapper classes should not be used in Java
What is Java <>?
What is Java
Eclipse does not start after Java 11 is installed
Why Java String typeclass comparison (==) cannot be used
[Java] Calculate the day of the week from the date (Calendar class is not used)
Frequently used Java generics
What is Java Encapsulation?
Yarn is not installed
[Java] When var should be used and when it should not be used
What is Java technology?
What is Java API-java
Why preventDefault is needed
[Java] What is JavaBeans?
[Java] What is ArrayList?
[Java] com.sun.glass.WindowEvent is imported and the window does not close
Java Object Serialization why & when
What is Java Assertion? Summary.
DateFormat is not thread safe
"tx" is not bound error
Ruby # {} is not variable expansion
java ArrayList, Vector, LinkedList comparison
Books used for learning Java
What is a Java collection?
[Windows] Java code is garbled
Why Kotlin is so useful
[Ruby] What is `!!` used for?
[Java] What is jaee j2ee?
[Java] What is class inheritance?
fields_for is used like this
[Rails] fields_for is not displayed
[Java basics] What is Class?
What is java escape analysis?
Java is the 5th day
Project facet Java version 13 is not supported. How to deal with