WHY IS JAVA VECTOR CLASS CONSIDERED OBSOLETE OR DEPRECATED?

Glen Martin

Total Post:27

Points:191
Posted by  Glen Martin
 1149  View(s)
Ratings:
Rate this:
Isn't its use valid when working with concurrency?

And if I don't want to manually synchronize objects and just want to use a thread-safe collection without needing to make fresh copies of the underlying array (as CopyOnWriteArrayList does), then is it fine to use Vector?

What about Stack, which is a subclass of Vector, what should I use instead of it?
  1. Mayank Tripathi

    Post:397

    Points:3117
    Re: Why is Java Vector class considered obsolete or deprecated?

    Vector synchronizes on each individual operation. That's almost never what you want to do.

    Generally you want to synchronize a whole sequence of operations. Synchronizing individual operations is both less safe (if you iterate over a Vector, for instance, you still need to take out a lock to avoid anyone else changing the collection at the same time, which would cause a ConcurrentModificationException in the iterating thread) but also slower (why take out a lock repeatedly when once will be enough)?

    Of course, it also has the overhead of locking even when you don't need to.

    Basically, it's a very flawed approach to synchronization in most situations. As MrSpandex pointed out, you can decorate a collection using the calls such as Collections.synchronizedList - the fact that Vector combines both the "resized array" collection implementation with the "synchronize every operation" bit is another example of poor design; the decoration approach gives cleaner separation of concerns.

    As for a Stack equivalent - I'd look at Deque/ArrayDeque to start with.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!