This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details
Why are the Java Vector (and Stack) classes considered obsolete or obsolete?
Why is Java Vector considered a legacy class (obsolete or obsolete)?
Is it invalid to use it when using concurrency?
Also, if I don’t want to manually synchronize objects and just want to use thread-safe collections without having to create a new copy of the underlying array (like CopyOnWriteArrayList), is Vector ok?
How about Stack, which is a subclass Vector, how should I use it instead of that?
Answer:
Vector synchronizes each individual operation. That’s almost never what you want to do.
Typically, you want to synchronize the entire sequence of operations. Synchronization of a single operation is not safe (Vector, for example, if the iteration, still need to lock, in order to avoid others change sets at the same time, otherwise it will cause ConcurrentModificationException iteration errors in the thread), but the synchronous speed also slower (why it repeatedly once is enough out of the lock?)
Of course, it has the overhead of locking even when you don’t need it.
Basically, this is a very flawed approach to synchronization in most cases. As Mr Brian Henk points out, you can use the following such a call to decorate the collection: Collections. SynchronizedList – Vector will “adjust the size of the array” collection implementation together with “synchronization of each operation” fact is another example of bad design; The decoration approach provides a clearer separation of concerns.
As for the Stack equivalents – I’ll look at Deque/ ArrayDeque first.
Answer:
Vector was part of 1.0 – the original implementation had two drawbacks:
1. Naming: A Vector is really just a list that can be accessed as an array, so it should be called ArrayList (which is the Java 1.2 Collections replacement Vector).
2. Concurrency: All get() and set() methods are synchronized, so you have no fine control over synchronization.
There’s not much difference between ArrayList and Vector, but you should use ArrayList.
From the API documentation.
Starting with the Java 2 platform V1.2, the class was improved to implement the List interface, making it a member of the Java Collections Framework. Unlike the new collection implementation, vectors are synchronous.Copy the code
The article translated from kgs4h5t57thfb6iyuz6dqtun5y ac4c6men2g7xr2a – stackoverflow – com. Translate. Goog/questions / 1…
** The authors suggest that synchronization cannot be finely controlled **
Thank you for reading this, if this article is well written and if you feel there is something to it
Ask for a thumbs up 👍 ask for attention ❤️ ask for share 👥 for 8 abs I really very useful!!
If there are any mistakes in this blog, please comment, thank you very much! ❤ ️ ❤ ️ ❤ ️ ❤ ️