在 Java 中我可以用什么来代替 Vector?

发布于 2024-10-20 16:48:08 字数 176 浏览 1 评论 0原文

看来在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

卷耳 2024-10-27 16:48:08

看起来在 Java 编程中,当涉及线程时,我们不应该再使用向量。

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

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

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

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

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

    • 该列表是线程限制的;即只有一个线程可以访问它。
    • 列表需要粗粒度同步;即执行一系列操作时的独占访问。在这种情况下,您通常会创建一个带有嵌入式(例如)ArrayList 的自定义类,该类不会在自定义类 API 中公开。
  • 如果用例需要细粒度同步,则 Collections.synchronizedList 包装器相当于 Vector。例如:

    列表<整数> vec = Collections.synchronizedList(new ArrayList());
    

    或者,您可以坚持使用 Vector 并避免使用 elements() 操作。

  • CopyOnWriteArrayList 列表的优点是它的迭代器支持并发修改......在某种意义上。如果您的应用程序主要执行读取列表的操作,它的扩展性也会更好。读取操作根本不需要显式同步,通常只需要读取单个易失性一次。但另一方面是写入操作确实同步,并且比“普通”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.

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. For example:

    List<Integer> vec = Collections.synchronizedList(new ArrayList<Integer>());
    

    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.

碍人泪离人颜 2024-10-27 16:48:08
List<String> list = Collections.synchronizedList(new ArrayList<String>());
List<String> list = Collections.synchronizedList(new ArrayList<String>());
话少情深 2024-10-27 16:48:08

对于线程不安全的情况,请使用ArrayList。

对于线程安全的情况,请使用最适合您情况的内容,CopyOnWriteArrayListQueueBlockingDeque 等。要提供更多建议,我们需要了解你如何用你的收藏来操纵事物。

我建议不要使用 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.

习ぎ惯性依靠 2024-10-27 16:48:08

数组,但如果您不知道大小,请使用并发映射的实现

array, but if you don't know the size, use an implementation of concurrentmap

昔日梦未散 2024-10-27 16:48:08

使用列表并像这样同步它们

List<?> list = Collections.synchronizedList(new ArrayList<?>());

Use Lists and synchronize them like

List<?> list = Collections.synchronizedList(new ArrayList<?>());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文