Vector is synchronized, ArrayList is not synchronized but we can synchronize an ArrayList by Collections.synchronizedList(aList)
, so which will perform better and faster?

- 62,887
- 36
- 269
- 388

- 4,629
- 8
- 37
- 52
-
1If this is C#, please tag your question with "C#" or ".NET". – FrustratedWithFormsDesigner May 21 '10 at 14:55
-
1Why don't you write a test and find out? – skaffman Jun 22 '10 at 18:17
-
Can you explain the usage pattern? 1) Many writes/many reads 2) Few writes, many reads, 3) Many writes, few reads 4) few/few doesn't need optimization – TJR Mar 30 '15 at 21:29
-
See also http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated – rogerdpack Jul 19 '16 at 19:54
1 Answers
Synchronized collections are a waste of time and dangerous. A trivial example why they are bad is to consider two threads running a loop at the same time on the same collection:
int i = 0;
while (i < list.size())
{
if (testSomeCondition(list.get())) {
list.remove(i);
else
i++;
}
Our list could be synchronized (e.g. a Vector) and this code would still break horribly. Why? Because the individual calls to size(), get(), remove(), are synchronized but one thread could still be removing items from the list while the other is iterating over it. In other words we have a race condition and the use of synchronized collections has gained us nothing.
To fix the race we have to synchronize the entire operation on the collection or use Java 5 concurrency Locks to do the same.
synchronized (list) {
int i = 0;
while (i < list.size())
{
if (testSomeCondition(list.get())) {
list.remove(i);
else
i++;
}
}
This block of code is now thread safe since only one thread can execute the loop at a time. And now there is no reason to use a synchronized collection. We can use an ArrayList instead of a Vector and save ourselves the performance penalty on all those synchronized calls.
So don't use synchronized collections. If you find yourself having multiple threads hitting the same list then you need to protect the operations on the list, not the individual calls.

- 5,809
- 3
- 33
- 38
-
18"Synchronized collections are a waste of time." -- too general. Synchronized collections have a purpose. You're just giving an example of how to use them wrong. Straw man argument. – thejoshwolfe Mar 19 '14 at 18:05
-
2They ARE a waste of time quite literally. The synchronized keyword imposes a call penalty even if the collection is used exclusively by a single thread. And most collections would be used exclusively by a single thread. And even if they're shared, then synchronizing an individual call still allows for race conditions so they are not fit for purpose. The developer just wastes THEIR time finding bugs in their supposedly thread safe collections. Simply put these collections are toxic and should never be used except when there is no avoiding them (e.g legacy cases, J2ME etc.). – locka Sep 30 '14 at 10:59
-
2Synchronization was slow in java 1.3 and earlier. In modern java it's better. http://www.ibm.com/developerworks/java/library/j-jtp04223/index.html Also, again with straw specious arguments. Nobody uses synchronization with a situation that is known to be single-threaded. Your first example is just plain bad programing and no collection can ever make up for that. You simply must think of what is or is not atomic regardless of what collection you use. Take a look at the code remove() in ArrayList and you will see why this method needs to be synchronized to maintain the integrity of the list. – Gus Jan 31 '15 at 18:03
-
1The world is filled with bad programming. The example is to trivially illustrate that synchronized collections are an attractive nuisance that can actually cause bugs - They look like they are safe but they aren't. It is more efficient, more obvious and safer to protect the entire operation and use unsynchronized collections inside. Clearly Sun recognized the problem with their original classes which is why they pushed them to the side in favour of unsynchronized versions. – locka Feb 02 '15 at 11:38
-
13This response doesn't answer the question. Instead it says don't worry about thread-safe collections. – TJR Mar 30 '15 at 21:30
-
1The answer makes sense by virtue of the fact that asking if Vector or a synchronized ArrayList is faster is the wrong question because there is no benefit to either. They're both dangerous. Don't use them. – locka Jan 21 '17 at 16:48
-
-