CopyOnWriteArrayList 或向量

发布于 2024-09-27 07:39:15 字数 149 浏览 2 评论 0原文

总之,

Vector类比ArrayList的优势在于它是同步的,因此保证了线程安全。然而,在 CopyOnWriteArrayList 和 Vector 之间,考虑到线程安全和性能,应该优先选择哪个。

All,

The edge Vector class has over ArrayList is that it is synchronized and hence ensures thread-safety. However, between CopyOnWriteArrayList and Vector, what should be the preferred considering thread safety and performance in consideration.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

蓝海 2024-10-04 07:39:15

这取决于使用模式 - 如果读取次数多于写入次数,请使用 CopyOnWriteArrayList,否则使用 Vector。

当 CopyOnWriteArrayList 的写入延迟较长(由于复制)但读取没有延迟时,Vector 会为每个操作引入较小的同步延迟。

另一个考虑因素是迭代器的行为 - Vector 在迭代时需要显式同步(因此写入操作不能同时执行),而 CopyOnWriteArrayList 则不需要。

It depends on the usage pattern - if you have much more reads than writes, use CopyOnWriteArrayList, otherwise use Vector.

Vector introduces a small synchronization delay for each operation, when CopyOnWriteArrayList has a longer delay for write (due to copying) but no delay for reads.

Another consideration is a behaviour of iterators - Vector requires explicit synchronization when you are iterating it (so write operations can't be executed at the same time), CopyOnWriteArrayList doesn't.

や莫失莫忘 2024-10-04 07:39:15

总的来说,这取决于读写操作的频率和性质,以及数组的大小。

您需要在您的环境中进行基准测试才能确定,但​​这里有一些一般原则:

  • 如果您只打算阅读
    array
    ,那么即使 ArrayList 也是
    线程安全(因为唯一的
    非线程安全的修改是
    那些修改列表的人)。因此
    你会想使用
    非同步数据结构,
    ArrayList 或
    CopyOnWriteArrayList 可能会
    工作同样出色。
  • 如果读取更常见
    与写入相比,那么你会
    倾向于更喜欢 CopyOnWriteArrayList,
    因为数组复制开销是
    仅在写入时发生。
  • 如果数组的大小是
    小,那么制作成本
    数组副本也会很小,
    因此这将有利于
    通过 Vector 进行 CopyOnWriteArrayList。

您可能还需要考虑其他两个选项:

  • 使用 ArrayList 但将同步放在其他地方以确保线程安全。这实际上是我个人最常使用的方法 - 基本上的想法是使用单独的、更高级别的锁来同时保护所有相关的数据结构。这比 Vector 那样对每个操作进行同步要高效得多。
  • 考虑一个不可变的持久数据结构 - 由于不可变性,这些保证是线程安全的,做不需要同步并且还受益于低开销(即它们在不同实例之间共享大部分数据而不是制作完整的新副本)。像 Clojure 这样的语言使用它们来获得类似 ArrayList 的性能,同时还保证完全的线程安全。

Overall, it depends on the frequency and nature of read and write operations, and the size of the array.

You'll need to benchmark in your context to be sure, but here are some general principles:

  • If you are only going to read the
    array
    , then even ArrayList is
    thread safe (since the only
    non-thread-safe modifications are
    those that modify the list). Hence
    you would want to use a
    non-synchronised data structure,
    either ArrayList or
    CopyOnWriteArrayList would probably
    work equally well.
  • If reads are much more common
    compared to writes
    then you would
    tend to prefer CopyOnWriteArrayList,
    since the array copying overhead is
    only incurred on writes.
  • If the size of the Array is
    small
    , then the cost of making
    array copies will also be small,
    hence this will favour
    CopyOnWriteArrayList over Vector.

You may also want to consider two other options:

  • Use an ArrayList but put the synchronisation elsewhere to ensure thread safety. This is actually the method I personally use most often - basically the idea is to use a separate, higher-level lock to protect all the relevant data structures at the same time. This is much more efficient than having synchronisation on every single operation as Vector does.
  • Consider an immutable persistent data structure - these are guaranteed to be thread safe due to immutability, do not require synchronisation and also benefit from low overhead (i.e. they share most data between different instances rather than making complete new copies). Languages like Clojure use these to get ArrayList-like performance while also guaranteeing full thread safety.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文