Vector vs SynchronizedList performance
Asked Answered
C

2

8

While reading Oracle tutorial on collections implementations, i found the following sentence :

If you need synchronization, a Vector will be slightly faster than an ArrayList synchronized with Collections.synchronizedList

source : List Implementations

but when searching for difference between them, many people discourage the using of Vector and should be replaced by SynchronizedList when the synchronization is needed. So which side has right to be followed ?

Chinkiang answered 1/1, 2016 at 14:29 Comment(6)
The performance difference, if any, and if it still exists since the time the article was written, is probably negligible. Vector should be considered as deprecated. Do as if it didn't exist. Moreover, Collections.synchronizedList() expresses the intent clearly: it says to the reader: I need a synchronized list here. Whereas new Vector says to the reader: this must have been written years ago, or by a programmer which learnt Java 17 years ago and never kept up-to-date.Plasia
@JBNizet Is there any example which shows how Vector is not suited for synchronization and SynchronizedList is doing the job better than it, because when i looked inside their codes both use synchronized blocs/methods.Chinkiang
It's not that one is synchronized and the other is not. I didn't say that. It's that Vector pre-dates the Collections API, brings a whole lot of methods that are obsolete, and is replaced by ArrayList and Collections.synchronizedList(arrayList). If it isn't officially deprecated yet, it's probably because it's still used in public APIs of other old classes of the JDK. But you should consider it as such.Plasia
@JBNizet thanks for your clarification, the tutorial is the one that should be updated.Chinkiang
@NarutoBijuMode Note: Vector was replaced in Java 1.2 (1998) It really is so old that you shouldn't be using it unless you have to.Edelmiraedelson
@PeterLawrey Thanks brother for your precious note.Chinkiang
H
2

When you use Collections.synchronizedList(new ArrayList<>()) you are separating the two implementation details. It’s clear, how you could change the underlying storage model from “array based” to, e.g. “linked nodes”, by simply replacing new ArrayList with new LinkedList without changing the synchronized decoration.

The statement that Vector “will be slightly faster” seems to be based on the fact that its use does not bear a delegation between the wrapper and the underlying storage, but deriving statements about the performance from that was even questionable by the time, when ArrayList and the synchronizedList wrapper were introduced.

It should be noted that when you are really concerned about the performance of a list accessed by multiple threads, you will use neither of these two alternatives. The idea of making a storage thread safe by making all access methods synchronized is flawed right from the start. Every operation that involves multiple access to the list, e.g. simple constructs like if(!list.contains(o)) list.add(o); or iterating over the list or even a simple Collections.swap(list, i, j); require additional manual synchronization to work correctly in a multi-threaded setup.

If you think it over, you will realize that most operations of a real life application consist of multiple access and therefore will require careful manual locking and the fact that every low level access method synchronizes additionally can not only take away performance, it’s also a disguise pretending a safety that isn’t there.

Handkerchief answered 13/5, 2016 at 11:24 Comment(0)
B
0

Vector is an old API, of course. No questions there.

For speed though, it might be only because the synchronized list involves extra method calls to reach and return the data since it is a "wrapper" on top of a list after all. That's all there is to it, imho.

Brasher answered 3/2, 2016 at 22:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.