This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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?? Cheers, John
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.