在 Java 中我可以用什么来代替 Vector?
看来在 Java 编程中,当涉及到线程时,我们不应该再使用向量了。
使用线程时应该使用什么类来代替 Vector?
import java.util.Vector;
Vector<String> v = new Vector<String>();
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>();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要了解为什么使用
Vector
在大多数情况下被认为是一件坏事。原因是:Vector
在每个操作上同步。大多数上下文不需要细粒度同步,因此这是不必要的性能开销。Vector.elements()
方法返回一个不具有快速失败语义的Enumeration
。回到你的问题。替代方案取决于您的线程尝试执行的操作:
如果用例根本不需要同步,请使用
ArrayList
或LinkedList
。您通常会在以下情况下使用它们:ArrayList
的自定义类,该类不会在自定义类 API 中公开。如果用例需要细粒度同步,则
Collections.synchronizedList
包装器相当于Vector
。例如:或者,您可以坚持使用
Vector
并避免使用elements()
操作。CopyOnWriteArrayList 列表的优点是它的迭代器支持并发修改......在某种意义上。如果您的应用程序主要执行读取列表的操作,它的扩展性也会更好。读取操作根本不需要显式同步,通常只需要读取单个
易失性
一次。但另一方面是写入操作确实同步,并且比“普通”ArrayList 昂贵得多。Vector
和Collections.synchronizedList
包装器的另一个问题是某些用例需要更粗略的同步;例如,测试列表的大小并在单个同步操作中有条件地添加元素。Queue
和Deque
类提供了处理此类事情的更高级别的抽象...对于涉及将工作从一个线程异步传递到另一个线程的用例。最重要的是,没有一种放之四海而皆准的解决方案。您需要了解应用程序设计的并发特性,并相应地选择数据结构。
最后,如果您正在为 Java ME 编程,您可能会不得不使用
Vector
,具体取决于您的目标 J2ME 配置文件。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: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
. For example:Alternatively, you could stick with
Vector
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 theCollections.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. TheQueue
andDeque
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.对于线程不安全的情况,请使用ArrayList。
对于线程安全的情况,请使用最适合您情况的内容,
CopyOnWriteArrayList
、Queue
、BlockingDeque
等。要提供更多建议,我们需要了解你如何用你的收藏来操纵事物。我建议不要使用
Collections.synchronizedList(...)
包装器,因为它可能无法很好地扩展(除非您不太关心可扩展性)。但这一切都取决于您的背景。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.数组,但如果您不知道大小,请使用并发映射的实现
array, but if you don't know the size, use an implementation of concurrentmap
使用列表并像这样同步它们
Use Lists and synchronize them like