在 Java 中选择最佳并发列表 [英] Choosing the best concurrency list in Java

查看:24
本文介绍了在 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 吗?并发 Queues 和 Maps 有更多选项(你可以从 Map 制作 Setss),并且这些结构往往最适合您要使用共享数据结构执行的许多类型的事情.

That said, are you sure you need it to be a List? There are a lot more options for concurrent Queues and Maps (and you can make Sets from Maps), 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:

这篇关于在 Java 中选择最佳并发列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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