File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Synchronising Vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Synchronising Vector" Watch "Synchronising Vector" New topic

Synchronising Vector

John Ryan
Ranch Hand

Joined: Mar 14, 2001
Posts: 124
Hi all,
I have some code that Im refactoring and have a query regarding the use of Vector. A general guideline for using Vector is that it should be used
1. When you need a Collection whose size can be determined dynamically
2. When that collection needs to be thread safe.
An ArrayList will have superior performance to a Vector because the ArrayList is unsynchronized. So if your not worried about multithreading an ArrayList would be preferable.
I have a class which may be accessed by multiple threads and which has a Vector as an instance variable. However within the methods where the Vector is structurally altered the code is synchronized on the Vector object. e.g

What Im wondering is if these code is correct.
It makes sense to synchronize on the myList object as we might want some of the processing code to be thread safe. However if each location where the Collection is altered is synchronized then does it make sense to use a Vector object?? Im using an object that is inherently thread safe yet Im still synchronising in each location that it is used. Is this adding unnecessary overhead?. Would it be the same to use an ArrayList??
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

Even for synchronization, Vector is no longer reccomended. You can obtain a Synchronized List by calling java.util.Collections.synchronizedList(someList).
As for your other questions, while synchronization is not my speciality, I would imagine that you shouldn't need to explicitly use the synchronized keyword everytime you use the list -- that's what obtaining the synchronized list does for you.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
I agree. Here's the link:
subject: Synchronising Vector
It's not a secret anymore!