本文介绍了在什么情况下CopyOnWriteArrayList适合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在了解类。 / p>


  • 复制新数组的目的是什么?

  • 是否为其他线程读取array?



因此,如果系统具有高并发性,并且大多数线程的操作都读取不写入, code> CopyOnWriteArrayList

解决方案

如:



是一个在Java 5中引入的并发Collection类并发API以及它的流行的表兄弟ConcurrentHashMap在Java。



CopyOnWriteArrayList实现了List接口,如ArrayList,Vector和LinkedList,但它是一个线程安全的集合,它以一种稍微不同于Vector或其他线程安全的方式实现了线程安全,安全集合类。

要获得最新版本,像 list.iterator();



话虽如此,更新这个集合alot会杀死性能。如果你试图对一个CopyOnWriteArrayList排序,你会看到列表抛出一个UsupportedOperationException(排序调用集合集合N次)。您应该在进行超过90 +%的读取时使用此读取。


I am learning about the CopyOnWriteArrayList class.

  • What is the purpose of copying a new array?
  • Is it for other threads to read the array?

So if a system has high concurrency and most of the threads' actions are reading not writing, it is better to use CopyOnWriteArrayList.

解决方案

As stated on this link:

CopyOnWriteArrayList is a concurrent Collection class introduced in Java 5 Concurrency API along with its popular cousin ConcurrentHashMap in Java.

CopyOnWriteArrayList implements List interface like ArrayList, Vector and LinkedList but its a thread-safe collection and it achieves its thread-safety in a slightly different way than Vector or other thread-safe collection class.

To get the most updated version do a new read like list.iterator();

That being said, updating this collection alot will kill performance. If you tried to sort a CopyOnWriteArrayList you'll see the list throws an UsupportedOperationException (the sort invokes set on the collection N times). You should only use this read when you are doing upwards of 90+% reads.

这篇关于在什么情况下CopyOnWriteArrayList适合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-31 03:37