我可以用什么来代替 Java 中的 Vector? [英] What can I use instead of a Vector in Java?

查看:58
本文介绍了我可以用什么来代替 Java 中的 Vector?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来在使用 Java 编程时,当涉及线程时,我们不应该再使用 Vectors.

在使用线程时应该使用什么类而不是 Vector?

import java.util.Vector;向量<字符串>v = new Vector();

解决方案

看起来在使用 Java 编程时,当涉及线程时,我们不应该再使用 Vectors.

您需要了解为什么使用 Vector 在大多数情况下被认为是一件坏事.原因如下:

  • Vector 在每个操作上同步.大多数上下文不需要细粒度的同步,因此这是一种不必要的性能开销.

  • Vector.elements() 方法返回一个没有快速失败语义的 Enumeration.

回到你的问题.替代方案取决于您的线程尝试执行的操作:

  • 如果用例根本不需要同步,请使用 ArrayListLinkedList.在以下情况下,您通常会使用这些:

    • 列表是线程限制的;即只有一个线程可以访问它.
    • 列表需要粗粒度同步;即在执行一系列操作时独占访问.在这种情况下,您通常会创建一个自定义类,其中包含未在自定义类 API 中公开的嵌入(比如)ArrayList.
  • 如果用例需要细粒度同步,Collections.synchronizedList 包装器等效于 Vector.或者,您可以坚持使用 Vector 并避免使用 elements() 操作.

  • CopyOnWriteArrayList 列表的优点是它的迭代器支持并发修改......在某种意义上.如果您的应用程序主要执行读取列表,它也会更好地扩展.读取操作根本不需要显式同步,通常只需要读取一个 volatile 一次.但另一方面是写操作会同步,并且比普通"ArrayList 的开销要高得多.

VectorCollections.synchronizedList 包装器的另一个问题是某些用例需要更粗略的同步;例如在单个同步操作中测试列表的大小并有条件地添加元素.QueueDeque 类提供了处理这种事情的更高级别的抽象......适用于涉及将工作从一个线程异步传递到另一个的用例.><小时>

最重要的是,没有一刀切的解决方案.您需要了解应用程序设计的并发特性,并相应地选择数据结构.

<小时>

最后,如果您正在为 Java ME 编程,您可能会坚持使用 Vector,具体取决于您所针对的 J2ME 配置文件.

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>();

解决方案

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 an Enumeration 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, or LinkedList. 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 a Vector. Alternatively, you could stick with Vector and avoid using the elements() 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 single volatile 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.

这篇关于我可以用什么来代替 Java 中的 Vector?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆