This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android 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: 1817

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.
subject: Synchronising Vector
Similar Threads
Collections Framework - use of Vector
i cant understand this code..
Generics query
Differences beteween ArrayList and Vector.
using collections in session object