Reputation: 1888
I was trying to understand why vector in Java has to be synchronized. Wherever I reached through google, they said that its synchronized because its elements are synchronized blocks other threads to access when one thread is accessing it. But what I really want to know is why it has to be? Java should have left it to the people who are accessing it. But why Java forced all the people accessing vector to use a synchronized data structure? I agree with people who says that ArrayList can be used for my requirement. But my point is what is that vector is going to give me??
Thanks and regards, Rengasami Ramanujam
Upvotes: 1
Views: 7773
Reputation: 677
If you want to synchronize the whole sequence of operations, use an arrayList. Synchronizing individual operators as vector does, is less safe.
If there is no need of thread-safe operations, it is recommended tu use ArrayLists.
Upvotes: 0
Reputation: 719229
But my point is what is that vector is going to give me??
It is a "legacy" class.
Unless you specifically need a synchronized list class, Vector
gives you nothing when you are writing new code.
However, Vector
is (still) there for a good reason. That reason is to allow you to run old Java code that was written to work with Java 1.0 and Java 1.1, or to run on some Java ME profiles that don't include the Java collections framework. And it has to (still) be synchronized because if they changed that, any multi-threaded legacy code that uses Vector
could break.
Upvotes: 2
Reputation: 44740
Consider this example that shows that you still need to use synchronization in your application code.
// BROKEN CODE, needs external synchronization
// only add an element if the vector is empty
if(vector.isEmpty())
vector.add(anElement);
Why Vector ? depends upon what you are doing
Internally, both the ArrayList and Vector hold onto their contents using an Array. You need to keep this fact in mind while using either in your programs. When you insert an element into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent. Depending on how you use these classes, you could end up taking a large performance hit while adding new elements. It's always best to set the object's initial capacity to the largest capacity that your program will need. By carefully setting the capacity, you can avoid paying the penalty needed to resize the internal array later. If you don't know how much data you'll have, but you do know the rate at which it grows, Vector does possess a slight advantage since you can set the increment value.
Upvotes: 1
Reputation: 308928
History.
Vector
was part of JDK 1.0; ArrayList
was not. We learned that synchronization as a default meant poor performance, so it was reversed when the Java Collections API came out.
You can still choose to synchronize any collection you need to using methods in java.util.Collections
, but it's your choice.
You should also be aware of the collections that are part of the java.util.concurrent
packages. Simultaneous access of collections certainly needs to be choreographed carefully.
Upvotes: 10
Reputation: 2164
Making Vector synchronised is just the way the people who write that Java library decided to implement it. Both ArrayList and Vector are implemented internally as arrays, both are dynamically resized but Vector is has a capacityIncrement
of double the current array size where for ArrayList it is half the current size. Vector is slower as you have to check is any other threads are accessing it before another thread does.
If you need a synchronised List data structure it is best to use synchronizedList(List list)
from the Collections API.
Upvotes: 0
Reputation: 12548
You are not forced to use a Vector
you are fine to use ArrayList
s as well.
If you work with threads you are happy if its synchronized. If your application doesn't use threading I would use something faster than a Vector
.
Upvotes: 0