Skip to main content

Posts

Showing posts from November, 2012

What does it mean by Vector being synchronized?

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 …