So you found that there is a very famous (and hence commonly found on all the question sites and hence not asked anywhere anymore) interview question: What is difference between Vector and ArrayList? And the answer is simple: Vector is synchronized but ArrayList is not.
Fine. But what does this mean?
If you understand how synchronized works I will tell you the simplest way to understand the difference. Just look at the implementation of Vector (here) and ArrayList (here).
In the implementation of Vector you will find that many of the methods are synchronized. size(), indexOf(), isEmpty(), set() etc. all are synchronized and hence all these methods can be accessed by one thread at a time. So the state of the Vector can be modified/accessed by only one thread at a time.
On the other hand, none of the methods in ArrayList are synchronized. This is what Java doc says:
Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list.
However for both Vector and ArrayList, the Iterators returned by Vector's/ArrayList's iterator and listIterator methods are fail-fast: if the Vector is structurally modified at any time after the Iterator is created, in any way except through the Iterator's own remove or add methods, the Iterator will throw a ConcurrentModificationException.
This clears the concept right?
Fine. But what does this mean?
If you understand how synchronized works I will tell you the simplest way to understand the difference. Just look at the implementation of Vector (here) and ArrayList (here).
In the implementation of Vector you will find that many of the methods are synchronized. size(), indexOf(), isEmpty(), set() etc. all are synchronized and hence all these methods can be accessed by one thread at a time. So the state of the Vector can be modified/accessed by only one thread at a time.
On the other hand, none of the methods in ArrayList are synchronized. This is what Java doc says:
Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list.
However for both Vector and ArrayList, the Iterators returned by Vector's/ArrayList's iterator and listIterator methods are fail-fast: if the Vector is structurally modified at any time after the Iterator is created, in any way except through the Iterator's own remove or add methods, the Iterator will throw a ConcurrentModificationException.
This clears the concept right?
Comments