What can I use instead of a Vector in Java?
Solution 1
It looks like when programming in Java we are not suppose to use Vectors anymore when threads are involved.
You need to understand why using Vector
is considered to be a bad thing in most contexts. The reasons are:
Vector
synchronizes on every operation. Most contexts do not require fine-grained synchronization, and as such it is an unwanted performance overhead.The
Vector.elements()
method returns anEnumeration
which does not have fail-fast semantics.
Bringing this back to your question. The alternatives depend on what your threads are trying to do:
If the use-case does not require synchronization at all, use
ArrayList
, orLinkedList
. You would typically use these if:- The list is thread-confined; i.e. only one thread ever can access it.
- The list requires coarse-grained synchronization; i.e. exclusive access while performing a sequence of operations. In this case, you would typically create a custom class with an embedded (say)
ArrayList
that is not exposed in the custom classes API.
If the use-case requires fine-grained synchronization,
Collections.synchronizedList
wrapper is equivalent to aVector
. Alternatively, you could stick withVector
and avoid using theelements()
operation.A
CopyOnWriteArrayList
list has the advantage that its iterator supports concurrent modification ... in a sense. It also scales better if your application mostly performs read the list. Read operations don't need to explicitly synchronize at all, and typically just need to read a singlevolatile
once. But the flip side is that write operations do synchronize, and are significantly more expensive than a "normal"ArrayList
.
The other problem with Vector
and the Collections.synchronizedList
wrapper is that some use-cases require coarser synchronization; e.g. testing a list's size and conditionally adding an element in a single synchronized operation. The Queue
and Deque
classes provide higher level abstractions that deal with this kind of thing ... for the use-cases involving passing work asynchronously from one thread to another.
The bottom line is that there is not one-size-fits-all solution. You need to understand the concurrency characteristics of your application design, and choose your data structures accordingly.
Finally, if you are programming for Java ME, you may be stuck with using Vector
, depending on what J2ME profile you are targeting.
Solution 2
For thread-unsafe cases, use ArrayList
.
For thread-safe cases, use whatever is most appropriate in your case, CopyOnWriteArrayList
, Queue
, BlockingDeque
, etc. To advice more, we need to know how you manipulate things with your collection.
I would recommend against using Collections.synchronizedList(...)
wrapper as it might not scale well enough (unless you don't care much about scalability). But it all depends on your context.
Solution 3
List<String> list = Collections.synchronizedList(new ArrayList<String>());
Admin
Updated on June 16, 2022Comments
-
Admin over 1 year
It looks like when programming in Java we are not suppose to use Vectors anymore when threads are involved.
What class should I use instead of a Vector when using threads?
import java.util.Vector; Vector<String> v = new Vector<String>();