在 Java 中选择最佳并发列表 [英] Choosing the best concurrency list in Java
问题描述
我的线程池有固定数量的线程.这些线程需要经常从共享列表中写入和读取.
My thread pool has a fixed number of threads. These threads need to write and read from a shared list frequently.
那么,在这种情况下,java.util.concurrent
包中的哪种数据结构(最好是 List,必须是无监视器的)是最好的?
So, which data structure (it better be a List, must be monitor-free) in java.util.concurrent
package is best in this case?
推荐答案
最好是
List
java.util.concurrent
中的唯一 List
实现是 CopyOnWriteArrayList.正如 Travis Webb 提到的,还有一个同步列表选项.
The only List
implementation in java.util.concurrent
is CopyOnWriteArrayList. There's also the option of a synchronized list as Travis Webb mentions.
也就是说,您确定需要将它作为 List
吗?并发 Queue
s 和 Map
s 有更多选项(你可以从 Map
制作 Set
ss),并且这些结构往往最适合您要使用共享数据结构执行的许多类型的事情.
That said, are you sure you need it to be a List
? There are a lot more options for concurrent Queue
s and Map
s (and you can make Set
s from Map
s), and those structures tend to make the most sense for many of the types of things you want to do with a shared data structure.
对于队列,您有很多选择,哪个最合适取决于您需要如何使用它:
For queues, you have a huge number of options and which is most appropriate depends on how you need to use it:
- ConcurrentLinkedQueue
- ArrayBlockingQueue
- LinkedBlockingDeque
- LinkedBlockingQueue
- PriorityBlockingQueue
- SynchronousQueue
- DelayQueue
这篇关于在 Java 中选择最佳并发列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!